Jump to content

How to include .tpl file in a .tpl file, from both the module and theme directory


Recommended Posts

hello,

I'm developping a new module.
My php files load the tpl files using the following syntax which is perfectly working :

if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/mymodule/view.tpl'))
   $smarty->display(_PS_THEME_DIR_.'modules/mymodule/view.tpl');
elseif (Tools::file_exists_cache(dirname(__FILE__).'/view.tpl'))
   $smarty->display(dirname(__FILE__).'/view.tpl');
else
   echo Tools::displayError('No template found');


This part of code check if the php has to use the tpl of the module or the tpl of the theme. Fine!

But within my view.tpl, I have the following line (which is working):

{include file="$tpl_dir./../../modules/mymodule/view-line.tpl"}


But in this case, it is not checking if there is a theme version of my .tpl file.

Any idea for making this part of code compatible with a theme version?

Many thanks,

Jerome

Link to comment
Share on other sites

Thanks for the reply but it is not working.
Here is the error message I get:

[sat Jul 09 10:40:04 2011] [error] [client 94.224.149.210] PHP Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template file '/home/prestashop/prestashop/themes/MyTheme/./view-line.tpl'' in /home/prestashop/prestashop/tools/smarty/sysplugins/smarty_internal_template.php:163\nStack trace:\n#0 /home/prestashop/prestashop/tools/smarty/sysplugins/smarty_internal_template.php(551): Smarty_Internal_Template->isExisting(true)\n#1 /home/prestashop/prestashop/tools/smarty/compile/89d396ba15aedc07ab68de4d679d1d813c204625.file.view.tpl.php(78): Smarty_Internal_Template->getRenderedTemplate()\n#2 /home/prestashop/prestashop/tools/smarty/sysplugins/smarty_internal_template.php(433): include('/home/prestasho...')\n#3 /home/prestashop/prestashop/tools/smarty/sysplugins/smarty_internal_template.php(568): Smarty_Internal_Template->renderTemplate()\n#4 /home/prestashop/prestashop/tools/smarty/Smarty.class.php(339): Smarty_Internal_Template->getRenderedTemplate()\n#5 /home/prestashop/prestashop/tools/smarty/Smarty.class.php(383): Smarty->fetch('/home/pr in /home/prestashop/prestashop/tools/smarty/sysplugins/smarty_internal_template.php on line 163, referer: http://localhost/modules/mymodule/view.php

Link to comment
Share on other sites

  • 3 weeks later...

You will see the solution if you look at the default category block in PrestaShop. On lines 280-283 (in PrestaShop v1.4.4) of modules/blockcategories/blockcategories.php you will see:

 

if (file_exists(_PS_THEME_DIR_.'modules/blockcategories/blockcategories_footer.tpl'))
   $smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl');
else
   $smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl');

 

This code checks whether an override exists and adds the right path in the branche_tpl_path variable. Then on line 36 of modules/blockcategories/blockcategories.tpl, you will see:

 

{include file="$branche_tpl_path" node=$child}

Link to comment
Share on other sites

  • 11 years later...

I fixed it this way:

In php files like the controller:

$this->context->smarty->assign([
    'module_dir' => _PS_MODULE_DIR_ . "{$this->module->name}/"
]);

$this->setTemplate("module:{$this->module->name}/views/templates/front/mixlist.tpl");

 

In tpl files:

{include file="{$module_dir}views/templates/front/_partials/miniatures/brand.tpl" brand=$brand}

 

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
×
×
  • Create New...