Jump to content

How do I organize a lot of templates?


Martin Muzatko
 Share

Recommended Posts

Hello! I am using ProcessWire for a good three years now already, and I am really happy with the flexibility it provides.

There is one thing though that bugs me. For a recent project, I need lots of templates.

I already have a site/templates folder polluted with a package.json, yarn.lock, node_modules, errors, .eslintrc, .git and my entire build setup.

And I thought: OK - when I am forced to use site/templates for templates, is it actually correct to put all my javascript, styles, components, functions, classes also there? I mean, at least this is what the default theme encourages (they put styles there also)

unspecified.thumb.jpg.dacd7a273e2ab291b0d7cf63fbd47fdb.jpgI read into other posts covering this topic about having a sub folder for templates. Because even with the non-template files stored somewhere else, the template files are still too many. Unfortunately, they also discovered that this is not possible:

Sub Directories for Templates

Should all template files put under site/templates folder

What are your ideas?

I already thought about having a page field, that defines the template path from a dropdown, but with this I would only reinvent an already almost working wheel.

Thank you for your inspiration.

 

Best,
Martin

Link to comment
Share on other sites

On 7/04/2017 at 0:08 AM, Martin Muzatko said:

And I thought: OK - when I am forced to use site/templates for templates, is it actually correct to put all my javascript, styles, components, functions, classes also there?

I think most people do, but you don't have to.

 

On 7/04/2017 at 0:08 AM, Martin Muzatko said:

I read into other posts covering this topic about having a sub folder for templates. Because even with the non-template files stored somewhere else, the template files are still too many. Unfortunately, they also discovered that this is not possible

You can do this using includes. For instance, you could have a template file "master.php", and set this file as the "Alternate template filename" for all your templates. Then in the master file you use some logic to include the file that contains the code for each template. For instance, you could create some subfolders to divide up these included files and then tag templates (on the Advanced tab of Edit Template) with a single tag according to which subfolder their file is in. Your logic in master.php then could be something like this:

$path = $page->template->name . '.php';
if($page->template->tags) $path = $page->template->tags . '/' . $path;
include($path);

 

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...