<p>Currently, the conditional redirect is the best way. You can use it to load a template dynamically in the structure's generic _entry template using the include tag.</p>

<p>My suggestion would be to add a lightswitch field that tells the entry to load a custom template. Then in the _entry template do something like this:</p>

<pre><code>{% if entry.customTemplate %}
    {% include "section/_" ~ entry.slug %}
{% else %}
    {% include "section/_generic" %}
{% endif %}
</code></pre>

<p>This is the same basic idea used in Pixel &amp; Tonic's help page on <a href="http://buildwithcraft.com/help/entry-type-templates">using different templates for different entry types</a>.</p>

CC BY-SA 3.0
2014-06-12T20:02:04.880Z
thanks bryan, this is close to what i did except that i managed to drop the conditional and use an array in the include in its place.
Ben Croker
CC BY-SA 3.0
2014-06-12T19:27:36.563Z
2014-06-12T19:27:36.563Z
Bryan Redeagle
<p>I think you are on the right track. Call the generic template <code>_route.html</code>, and have it do nothing but <code>{%extends ...%}</code> to route to the template you want.</p>

CC BY-SA 3.0
2014-06-12T19:30:25.263Z
2014-06-12T19:30:25.263Z
Marion Newlevant
<p>The cleanest way I have found to achieve this is to create a generic template for the structure as well as a custom template for each entry that needs one, ensuring that its path matches the entry's uri. </p>

<p>I then put this single line of code in the structure's selected template and it works beautifully:</p>

<pre><code>{% extends [entry.uri, '_generic'] %}
</code></pre>

<p>This should work as well:</p>

<pre><code>{% include [entry.uri, '_generic'] %}
</code></pre>

<p>If the entry's uri exists as a template route then it will be rendered, otherwise the generic template will be.</p>

<p><strong>UPDATE:</strong></p>

<p>I recently extended this on a site to allow for unique custom pages, custom entry type pages and a fallback generic page:</p>

<pre><code>{% extends [
    '_custom/' ~ entry.slug,
    '_entrytypes/' ~ entry.type,
    '_generic'
] %}
</code></pre>

CC BY-SA 3.0
2017-02-24T14:20:22.600Z
If your entries are in Structure, and some pages need to use different template, you can use this code in your 'main' template defined in section settings. ``{% extends [
    craft.request.path,
    '_generic'
] %}``
Yuri Salimovskiy
CC BY-SA 3.0
2014-06-30T17:58:46.680Z
Im having a bit of a problem managing this method where  the entry.uri file needs to extend blocks in the main file.  Any ideas?
Steve Adams
CC BY-SA 3.0
2014-12-17T19:26:29.103Z
no, not in the scenario above
Ben Croker
CC BY-SA 3.0
2014-06-12T19:46:03.940Z
That's very clever! Per the Twig docs, "The first template that exists will be used as a parent".
http://twig.sensiolabs.org/doc/tags/extends.html#dynamic-inheritance
Lindsey D
CC BY-SA 3.0
2014-06-12T20:23:15.837Z
In my opinion, this should be marked as the best answer. You may also want to share this "recipe" on http://craftcookbook.net/
Lindsey D
CC BY-SA 3.0
2014-06-12T19:41:16.933Z
2015-05-28T15:25:12.867Z
2015-05-28T15:25:12.867Z
Ben Croker
<p>While setting up a site recently I wanted to route a specific entry in a structure section to its own custom template. Using a dynamic route sounded like the right option until I remembered that it wouldn't work due to Craft's routing order (matched entry uri comes before matched template). </p>

<p>I considered adding a conditional redirect to the structure's generic template but this feels a bit dirty. Does anyone have a better solution of how to make this work?</p>

<templating><structure><custom><routing>
How can I route a specific entry in a structure section to its own template?
# How can I route a specific entry in a structure section to its own template?

(Asked by: username_0 on 2014-06-12)

While setting up a site recently I wanted to route a specific entry in a structure section to its own custom template. Using a dynamic route sounded like the right option until I remembered that it wouldn't work due to Craft's routing order (matched entry uri comes before matched template).

I considered adding a conditional redirect to the structure's generic template but this feels a bit dirty. Does anyone have a better solution of how to make this work?

**Tags:** templating, structure, custom, routing


---

## 3 answers

### Answer by username_0 on 2014-06-12. Score: 44 | Accepted answer

The cleanest way I have found to achieve this is to create a generic template for the structure as well as a custom template for each entry that needs one, ensuring that its path matches the entry's uri. 
I then put this single line of code in the structure's selected template and it works beautifully:
{% extends [entry.uri, '_generic'] %}

This should work as well:
{% include [entry.uri, '_generic'] %}

If the entry's uri exists as a template route then it will be rendered, otherwise the generic template will be.
UPDATE:
I recently extended this on a site to allow for unique custom pages, custom entry type pages and a fallback generic page:
{% extends [
    '_custom/' ~ entry.slug,
    '_entrytypes/' ~ entry.type,
    '_generic'
] %}

> **Comments:**
> **username_5**: That's very clever! Per the Twig docs, "The first template that exists will be used as a parent". http://twig.sensiolabs.org/doc/tags/extends.html#dynamic-inheritance  
> **username_5**: In my opinion, this should be marked as the best answer. You may also want to share this "recipe" on http://craftcookbook.net/  
> **username_4**: Im having a bit of a problem managing this method where  the entry.uri file needs to extend blocks in the main file.  Any ideas?  
> **username_0**: no, not in the scenario above  
> **username_3**: If your entries are in Structure, and some pages need to use different template, you can use this code in your 'main' template defined in section settings. ``{% extends [     craft.request.path,     '_generic' ] %}``  

---

### Answer by username_1 on 2014-06-12. Score: 5

Currently, the conditional redirect is the best way. You can use it to load a template dynamically in the structure's generic _entry template using the include tag.
My suggestion would be to add a lightswitch field that tells the entry to load a custom template. Then in the _entry template do something like this:
{% if entry.customTemplate %}
    {% include "section/_" ~ entry.slug %}
{% else %}
    {% include "section/_generic" %}
{% endif %}

This is the same basic idea used in Pixel & Tonic's help page on using different templates for different entry types.

> **Comments:**
> **username_0**: thanks bryan, this is close to what i did except that i managed to drop the conditional and use an array in the include in its place.  

---

### Answer by username_2 on 2014-06-12. Score: 1

I think you are on the right track. Call the generic template `_route.html`, and have it do nothing but `{%extends ...%}` to route to the template you want.


---

