Jump to content

Different templates-folder for different users/roles?


pideluxe
 Share

Recommended Posts

Would it be possible to configure a different path to the templates for different users/roles? E.g. for the user admin the templates reside in the folder /site/dev-templates/  and for all others users the templates lie in the folder /site/templates/?

With this, you could have a production-version of your templates and when logging in as a special user you can work on your development-version simultaneously. Maybe there could be a function to check-in ready templates to the production-version.

Link to comment
Share on other sites

Raymond Geerts, has pointed me a way to swap templates folder based on the hostname. (His post is somewhere here on the forum.) You need to place this code in /site/config.inc
Here's my variation:

/**
 * Switch ProcessWire templates directory on $_SERVER['HTTP_HOST']. Type the
 * Hostname as key and the name of the new templates folder as value.
 *
 */

$config->templates = array(
    'sub.domain.dev' => 'templates-dev', // domain => templates folder name
);

if (isset($_SERVER['HTTP_HOST']) && isset($config->templates[$_SERVER['HTTP_HOST']], $config->templates)) {
    foreach ($config->templates as $host => $folder) {
        if ($_SERVER['HTTP_HOST'] === $host) {
            $config->urls->templates = "/site/" . $folder . "/";
            $config->paths->templates = dirname(__DIR__) . $config->urls->templates;
        }
    }
} 

Here is Raymond's post.

Edited by Martijn Geerts
  • Like 6
Link to comment
Share on other sites

You should be able to do this on your own. Use $config->prependTemplateFile = '_init.php'; in your config.php and try this:

// _init.php
// Alter from basic-page.php to basic-page-dev.php

if($user->isSuperuser()){
  $page->template->altFilename($page->template->name."-dev");
}
  • Like 2
Link to comment
Share on other sites

i use that technique from Raymond and i gotta say, it's pure gold!; saved me many times and helped along a project that i thought would be a nightmare to work on (requirement to work on site while it was in use)..

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

I know this is old, but just a follow up to mention that Tracy Debugger has a few options for achieving similar functionality.

  1. Files Editor panel lets you test changes without affecting what other users see. When ready you can push the changes live.
  2. Template Path panel lets you choose from a variety of templates with -dev, -test etc suffixes.
  3. The User Bar has a "Page Versions" component that lets authorized users choose from alternate templates (this is basically a non-superuser version of the Template Path panel.
  4. User Dev Template Options - this lets you set users with certain roles to automatically see the page with a different version of the template file.

Anyway, thought some of these might be useful to others who come across this topic.

  • Like 7
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...