core
core copied to clipboard
better template organization / optimize clarity
Currently all templates for a theme are stored in one folder (which can be selected in the theme settings) and can only be organized by a very good system for their file names. I understand that subfolders are for different themes but isn't there a way to optimize the whole approach?
Especially when using complex frameworks (e.g. bootstrap) or extensions (e.g. metamodels) the template folder is a mess, regardless of any file naming system.
It would be great for a lot of projects and people to have a possibility to organize the templates better, f.e. by using subfolders and scanning them recursively for one specific theme, using labels or anything that allows me to do something similiar to this:
/templates ----/myTheme (select this folder in theme settings) --------/navigation (templates in this folder can be selected in every module) --------/metamodels (templates in this folder can be selected in every module) --------/calendar ("-") --------/newsletter ("-") ----/otherTheme --------/navigation --------/metamodels --------/calendar --------/newsletter --------/and so on...
Additionally I want to point out, that the filter search for name seems not to work.
true, tested in 4.4.3
This issue belongs to the contao/core-bundle repository since Contao 3.5 is feature complete.
Sorry, I'm new to the github workflow. What am I supposed to do? Should I report the issue / feature request there?
Don't worry about it. I think Leo (or the issue-bot) will move the ticket accordingly.
Ok please also move the feature request regarding the template organisation and not only the issue with the filter search if possible, thanks very much :)
The reason why we are not supporting nested folders in the custom templates directory is the file_exists() call, which is executed one time for each template:
https://github.com/contao/core/blob/master/system/modules/core/library/Contao/TemplateLoader.php#L103
The same applies to the /templates directory BTW.
@contao/developers I understand the usability problem but I also think that a recursive file search will noticeably affect the performance. What do you think?
Maybe you can class-inherit the first part of the templatename (like fe_, ce_, mod_, news_ cal_, block_, form_, ... ) and use this as a subfolder 🤔
I think it would be very easy to implement if we added all the templates to the cache as well. Which would btw also make it faster. Here are the two options
-
we leave it as is.
- Advantages: A new or renamed template is always loaded live so it's developer friendly during development.
- Disadvantages: It's slower in production and it doesn't allow a nested structure because that would make it even slower. Also, it's inconsistent because if you place a template in
system/modules/whatever/templatesorvendor/...you need to clear the cache to make it work, if it's in/templatesit's loaded live.
-
we add the
/templatesto the template cache as well.- Advantages: Works the same as templates in modules/bundles. It's way faster in production. It allows for arbitrary nesting levels.
- Disadvantages: You have to clear and rebuild the cache every time you rename or add a new template (note: if you change the content, that's no problem! It's just about the paths) or make sure you work in the development mode
app_dev.php. We might add a hint in the back end if you're navigating there in production mode.
Needless to say that I fancy version 2. In my opinion everything should be optimized for production.
If you allow arbitary nesting how would you prevent possible inconsitencies?
like templates/themeA/news_full.html5vs templates/themeA/news/news_full.html5
Same as in all other modules too. The last one wins.
Since I'm not a real developer I just want to add something to the discussion from the usability point of view... how about a small update for the whole "Templates" area which includes:
- (if inconsistencies, @asaage ) an info box where current inconsistencies are listed
- (if inconsistencies @asaage , @Toflar ) something where the user gets an explanation that the most recent template is used, the name of this template and its path and other templates with the same name
- (@Toflar ) a toggle for activating / deactivating the cache for all the templates and some info for the user what this means for his production / dev work OR just a button to rebuild the cache with some information what this means
and for all templates in the templates list some extra information for each template (in a new column or in brackets behind the template name or whatever):
- name and ID of the module that is using this template right now and a quick link to this module
- some space for a short comment to describe the template
Just some thoughts since I'm working with Contao for years now and in my opinion the templates area needs an upgrade. Espacially when using 3rd party modules you have a lot of templates and need a better management and performance.
Short: Implementing @Toflar's ideas inlcuding cache for /templates and last one wins and wrapping them in a small and usable template manager would be a real improvement :)
As discussed in Mumble on September 7th, having subfolders is something we want to support. To accomplish this, we have to add the custom templates to the template cache. The cache then needs to be updated (in an atomic operation) if a new file is added or an existing one is renamed or removed.