Multiple templates directories

Hi everyone,

I’m moving our OJS installation from 2.4.2 to 2.4.6. and I think I may have found a very easy way to add multiple templates directories as asked here and here.

Seems to work well but I’m a bit concerned about unexpected issues.

My changes:

  • I've created a new folder called "override" and inside this one, another called "templates"
  • I've made two small changes to PKPTemplateManager.inc.php file:
    • Added this line of code before line 82
      $this->over_template_dir = $baseDir . DIRECTORY_SEPARATOR . 'override' . DIRECTORY_SEPARATOR . 'templates';
      
    • Changed line 86 to:
      $this->template_dir = array($this->over_template_dir, $this->app_template_dir, $this->core_template_dir);
      
So it's just one modified line of code and another one added to PKPTemplateManager.inc.php. Smarty seems to check first if file exists on "override > templates", then on "templates" and finally on "lib > pkp > templates". All compiled and cache templates share already defined directories, no changes there.

My questions are: Does this involve any security or unexpected issues? Could this be moved to a plugin that extends PKPTemplateManager?

I’ve been testing it and have not found anything wrong so far. It allows to have all changes separated from the core files, except this one.

Thanks & cheers,
Andy

PS:

@beghelli pointed me in another topic to his great template manager plugin ( http://lib-git.lib.sfu.ca/beghelli/customTemplateManager) but I’ve got 206 changed files so I thought this way (multiple directories) would be easier for me to maintain or adjust our modifications during upgrades.

I also know OJS 3.0a will be available on August and it completely changes templates management but this may be helpful for 2.4 installations (if there’s no issues…).

1 Like

Hi @andymp ,

We did something similar for OJS 3.0; see:

I think your approach should work well, though you might want to check what happens when filenames clash – e.g. whether recompiles are triggered as expected by template file modifications. I remember running into a few small issues there.

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Thanks @asmecher! Lot’s of changes there!

Regarding what you said, I’ve been searching for a solution and it seems recompiles should be correctly triggered by template file modifications if there’s a compile_id assigned. And I think I’ve found the code I needed here: framework/Smarty.php at master · joestump/framework · GitHub

So, added to the changes mentioned before, I’ve changed the fetch function this way:

  • Line 283 changed to:
function fetch($resource_name, $cache_id = null, $compile_id = '', $display = false)
  • Added new line before returning this function:
$compile_id = $this->getCompileID($resource_name, $compile_id);

And I added a new function:


 private function getCompileID($template, $compile_id)
    {
        if (strlen($compile_id)) {
            return $compile_id;
        }
        $compile_id = $this->template_dir . $template;
        return sha1($compile_id);
    }

Don’t know if this solves anything at all, but everything seems to work well right now. I’m going to continue with the tests and I’ll let you know if I find any issue.

Thanks again Alec!
Cheers,
Andy

1 Like

Hi @andymp,

Ah, good spotting. I’ve used this approach in the master branch with a couple small distinctions from your suggestion:

Regards,
Alec Smecher
Public Knowledge Project Team

2 Likes

Hi @asmecher,

Nice!!! Your code is really clean, so I’m going to replicate your changes to see how they work on my installation.

Thanks & regards,
Andy

1 Like

Using your override/templates folder example:

If i wanna to make a change in the footer.tpl where do i put it?

inside:

override/templates/footer.tpl

or

override/templates/lib/pkp/templates/commom/footer.tpl ?

Thanks