ryancramerdesign / ProcessWire

Our repository has moved to https://github.com/processwire – please head there for the latest version.
https://processwire.com
Other
727 stars 198 forks source link

feature request - template files in subdirs #539

Open pine3ree opened 10 years ago

pine3ree commented 10 years ago

Hello Ryan, would it be possible to add the choice of assigning template files like

catalog/product.php catalog/category.php catalog/category/widget.php ....

kind regards

ryancramerdesign commented 10 years ago

We are trying to maintain a simple and direct mapping of template names to PHP filenames in /site/templates/ only. But it's possible for you to manage your own level of templates relatively easily. In your template settings Advanced tab, you have the ability to specify an alternate template filename. That filename has to be in /site/templates/, but it can itself contain the logic to map to your structure of choice. There are many ways you can map things. In your case, I'd suggest creating a single template called controller.php (or whatever you want to call it) and then setting that as your template file for templates: catalog-product, catalog-category, and catalog-category-widget. Then in your category.php template file:

<?php
$file = './' . implode('/', explode('-', $page->template->name)) . '.php';
if(file_exists($file)) {
  include($file); 
} else {
  echo "Template file not found: $file";
}
pine3ree commented 10 years ago

Thank You Ryan, of course i was already doing that. I use template files as controllers in a std mvc app. And i use hypens to separate namespaces in templates files, pretty much like in your example. But then we could end up with a lot of file in the templates dir. Now I am trying a different approach: i use a single controller per area(blog, services etc) (most of the area related templates are mapped to this template file) which renders most of the time a single layout. Inside the layout i combine partials for standard page elements (header, footer, sidebars...) and a main partial which use templates files inside directories (as blog/post-list.phtml, blog/post.phtml, blog/widget/recent-posts.phtml etc etc) according to the page template. So my problem is solved. I was just thinking that being able to also map directly into subirectories could be useful to keep things cleaner in the filesystem. I hope i will be able to look into it next week and see if i can make a module to do that.

kind regards

pine3ree commented 10 years ago

oh. it just came to my mind. It think the main problem in the admin will be to have the template file autodiscovery feature checking subdirs (well not a real problem, but a boring feature to implement...)