Jump to content

should all template files put under site/templates folder ?


adrianmak
 Share

Recommended Posts

Basicly you have one folder for templates by default. This is configured to be in "site root" + templates/

You are able to overrule this setting from the site/config.php as follow

$config->urls->templates = $config->urls->site . 'mytemplates/';
$config->paths->templates = $config->paths->site . 'mytemplates/';

A trick i use a lot when developing in a live site is to duplicate the template folder and rename the copy to templates-dev. Then make sure i have an other domain (or subdomain) pointing to the hosting, for example dev.domain.ext and for the live site on www.domain.ext

With the following code i can access the development templates while normal visitors keep getting the default templates.

/**
* Development: Alias for template-dev access
*
*/
if($_SERVER['HTTP_HOST'] == 'dev.domain.ext') {
    $config->urls->templates = $config->urls->site . 'templates-dev/';
    $config->paths->templates = $config->paths->site . 'templates-dev/';
    $config->debug = true;
}

You can make up all kind of 'rules' to go by when overruling the templates folder with php from the config.php file.

For example serving the same site with different layout and code for mobile on m.domain.ext

/**
* Mobile: Alias for template-mobile access
*
*/
if($_SERVER['HTTP_HOST'] == 'm.domain.ext') {
    $config->urls->templates = $config->urls->site . 'templates-mobile/';
    $config->paths->templates = $config->paths->site . 'templates-mobile/';
}
  • Like 25
Link to comment
Share on other sites

I think maybe the original question was not about changing the path to the folder where templates reside, but rather about being able to put template files in different subfolders of site/templates (or something different, adjusted with the methods, described above).

Although you can change the file used as template file for the template in Admin -> Setup -> Templates -> Edit Template on the Files tab, you cannot point to something like "views/product/product.php" there as far as I know (at least it did not work when I tried). You can only choose different file in template folder. It could be just great if you could.

  • Like 2
Link to comment
Share on other sites

  • 4 years later...
On 1/15/2015 at 5:38 PM, Ivan Gretsky said:

you cannot point to something like "views/product/product.php" there as far as I know

Just want to add for anyone reading this post, that while in the admin this cannot be done, it is doable via code. By putting something like this into init.php,:

$templates->get('home')->filename = $config->paths->templates . 'views/home/home.php';

it is possible to store a template file wherever you want.

Read more about this in this topic: https://processwire.com/talk/topic/2676-configuring-template-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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...