<p>Ultimately I had to take approach similar to recommended by @crawf (though I don't completely like it).</p>

<p>So I added "generic" route</p>

<pre><code>'reports/csv/(.*)'         =&gt; 'reports/csv',
</code></pre>

<p>Then, in my <code>reports/csv/index.html</code> template I have this code:</p>

<p><code>{% include "reports/csv/"~craft.request.segment(3) %}</code></p>

<p>And then I created other templates in the folder (to be included into the main one), with names matching 3rd URL segment.</p>

<p>This way, there is only 1 check and include, and it's kinda 'dynamic'. I would still prefer avoiding this include for performance sake, but it's still better then having to add route for each new template.</p>

CC BY-SA 3.0
2017-02-23T15:38:12.723Z
2017-02-24T07:23:11.883Z
2017-02-24T07:23:11.883Z
Yuri Salimovskiy
<p>What we often do for a section (lets call it Pages) is actually not use any custom routing, and control things in the templates.</p>

<p>Start by setting the section template to <code>pages/index</code>. In your <code>index.html</code> file, we usually have the following:</p>

<pre><code>{# Look for a template for this entry type (if any), then look for a template matching #}
{# the entry's slug, and fallback to the default '_entry' template #}

{% include [
    'pages/' ~ entry.type,
    'pages/' ~ entry.slug,
    'pages/_entry'
] %}
</code></pre>

<p>Using Twig's array-style include means it'll look for templates, and if one doesn't exist, move onto the next. In this scenario, we can simply create a file for an entry type, or even for a specific slug, with a fallback to a default.</p>

<p>Your case might be different of course, but might be a step in the right direction.</p>

CC BY-SA 3.0
2017-02-18T10:01:27.137Z
This is interesting approach, however I prefer having more straightforward template structure. From my expirience, having many includes makes hard finding the appropriate templates when it comes to making changes later when you start to forget what belongs where. Thank you for the idea anyway.
Yuri Salimovskiy
CC BY-SA 3.0
2017-02-17T11:54:29.927Z
2017-02-17T11:54:29.927Z
crawf
<p>I have many of the templates organized into sub-folders. E.g. <code>/manager/entries/edit.html</code>, <code>/manager/users/edit.html</code> etc.</p>

<p>The site is using 4-segment URLs to access code in those templates, e.g. <code>http://site.com/manager/entries/edit/{slug}</code>, <code>http://site.com/manager/users/edit/{id}</code> etc.</p>

<p>Currently I have to set up custom route for every template, e.g.     <code>'manager/users/edit/(.*)'        =&gt; 'manager/users/edit',</code> etc.</p>

<p>Is there a way I could set up a "generic" route which would first check for existing templates in a subfolder (whether globally or on per-folder basis) and try to use that first? So I would not need to create route for each individual template.</p>

<url><routes><routing>
Catch-all routes based on template structure
# Catch-all routes based on template structure

(Asked by: username_0 on 2017-02-17)

I have many of the templates organized into sub-folders. E.g. `/manager/entries/edit.html`, `/manager/users/edit.html` etc.

The site is using 4-segment URLs to access code in those templates, e.g. `http://site.com/manager/entries/edit/{slug}`, `http://site.com/manager/users/edit/{id}` etc.

Currently I have to set up custom route for every template, e.g. `'manager/users/edit/(.*)' => 'manager/users/edit',` etc.

Is there a way I could set up a "generic" route which would first check for existing templates in a subfolder (whether globally or on per-folder basis) and try to use that first? So I would not need to create route for each individual template.

**Tags:** url, routes, routing

---

## 2 answers

### Answer by username_0 on 2017-02-23. Score: 1 | Accepted answer

Ultimately I had to take approach similar to recommended by @crawf (though I don't completely like it).
So I added "generic" route
'reports/csv/(.*)'         => 'reports/csv',

Then, in my reports/csv/index.html template I have this code:
{% include "reports/csv/"~craft.request.segment(3) %}
And then I created other templates in the folder (to be included into the main one), with names matching 3rd URL segment.
This way, there is only 1 check and include, and it's kinda 'dynamic'. I would still prefer avoiding this include for performance sake, but it's still better then having to add route for each new template.


---

### Answer by username_1 on 2017-02-17. Score: 2

What we often do for a section (lets call it Pages) is actually not use any custom routing, and control things in the templates.
Start by setting the section template to pages/index. In your index.html file, we usually have the following:
{# Look for a template for this entry type (if any), then look for a template matching #}
{# the entry's slug, and fallback to the default '_entry' template #}

{% include [
    'pages/' ~ entry.type,
    'pages/' ~ entry.slug,
    'pages/_entry'
] %}

Using Twig's array-style include means it'll look for templates, and if one doesn't exist, move onto the next. In this scenario, we can simply create a file for an entry type, or even for a specific slug, with a fallback to a default.
Your case might be different of course, but might be a step in the right direction.

> **Comments:**
> **username_0**: This is interesting approach, however I prefer having more straightforward template structure. From my expirience, having many includes makes hard finding the appropriate templates when it comes to making changes later when you start to forget what belongs where. Thank you for the idea anyway.  

---

