Using templates in themes

Hi,

I have looked for a solution to add custom template files to themes which would bypass the core templates. I know that this will be available in 3.0, but I also discovered a fairly easy solution to use in 2.4. However, I would like to know if anyone knows of any problems with this solution?

I simply add this code to the theme plugin:

function activate(&$templateMgr) {

        $theme_template_id = "myThemeName";
        $theme_template_dir = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $theme_template_id . DIRECTORY_SEPARATOR . 'templates';
        $app_template_dir = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'templates';
        $core_template_dir = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'templates';    
        
        $templateMgr->template_dir = array($theme_template_dir, $app_template_dir, $core_template_dir);                                                  
        $templateMgr->compile_id = $theme_template_id;
        
        if (($stylesheetFilename = $this->getStylesheetFilename()) != null) {
            $path = Request::getBaseUrl() . '/' . $this->getPluginPath() . '/' . $stylesheetFilename;
            $templateMgr->addStyleSheet($path);
        }
        
        
    }

After adding this I can copy a single .tpl file to the theme folder and it will bypass the core file. Of course I have to use the same folder structure. Also, I do not have to make any changes to the core, everything is handled through the theme plugin.

But as I said, I would be happy to hear if there are any problems with this solution?

Hi @ajnyga,

That’s basically the solution we’ll be supporting going forward in OJS 3. You may need to manually flush the cache when you modify a file – I don’t recall if Smarty handles this well by default – but overall I think that’s a good solution.

Regards,
Alec Smecher
Public Knowledge Project Team

I know I’m digging old topic up but we’re still using 2.4.x for now and I just wanted to clarify:

So the code goes into the MyThemePluging.inc.php file and you have to recreate the entire directory structure?

So if you wanted to replace header.tpl you’d have to create under plugins/themes/MyTheme the structure lib/pkp/templates/common/header.tpl?

Hi @tauyeung,

For OJS 2.x, you’d need to apply the changes suggested above, and my understanding is that they’d behave as you describe.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi,

Yes, just like you thought. After writing that piece, I have seen a lot of similar solutions elsewhere. Don’t remember exactly where, but the idea has been the same.

Hi,

Would this solution still work for OJS 3?

If not, is there a different (easier) way of implementing your own custom theme with templates?

Hi @DaniilMaslov

Yes, there is a solution for theming in OJS 3, please see https://www.gitbook.com/book/pkp/pkp-theming-guide/details.

Best,
Bozana