Suppose I have a section for Products, that uses the template at products/_entry and is set up to have the URL format of 'products/{slug}'.

Our existing page structure shows a Product page when you go to the URL (such as example.com/products/cool_product, but we also have a sub-page for "Product Details" which we would get to (using the previous example) by going to example.com/products/cool_product/details.

Is something like this possible with Craft routes? I've tried adding a route in craft/config/routes.php that looks like:

return array(
    'products/<slug>/details' => 'products/_details'

however that doesn't do what I'd expect, which is load the product entry using that slug and display the template (which would in turn display the relevant data from that product entry)

Do I need to have something in my _layout.html that catches such a request somehow? I'm a bit confused I guess. I suppose I can handle the request a bit more manually in the template by parsing the request and loading the product that way. I guess I'm hoping there is something a little more elegant (read: I'm lazy )

1 Answer 1


You might check out the routing docs on accessing subpatterns in your templates.

Here is the example given in the docs (which provides the variables 'year' and 'month' to the template that correspond to the matched uri segments — which would match `news/2015/02'):

'news/(?P<year>\d{4})/(?P<month>\d{2})' => 'news/_archive',

So I would presume you could do the same using your products (I'm terrible with regular expression though, so please check the match syntax).

'products/(?P<slug>[^/]+)/details' => 'products/_details',

Fyi, the variable is defined using (?P<name>...), where '...' is the pattern to match. So in this case the variable 'slug' (or whatever you defined it as in your regex pattern) should now be available as a variable in your template, which you can use to retrieve the entry.

{% set entry = craft.entries.section('mySectionHandle').slug(slug).first() %}
{% if entry %}
    {{ entry.title }}
{% endif %}

When you create the link to that page you would naturally want to include the slug:

<a href="products/{{ entry.slug }}/details">View details</a>


  • This worked! I guess it was easier than I first thought. The only thing I had to do different was {% set entry = craft.entries.section('mySectionHandle').slug(slug).first() %}
    – Chad W
    Feb 18, 2015 at 17:55
  • Great! Ya... I couldn't remember if using slug returned an ElementCriteriaModel or EntryModel. I should have warned you. Glad you got it working. I updated the answer. Feb 18, 2015 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.