3

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.

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.

  • 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 Feb 18 '17 at 10:01
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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