We often help find solutions for frequently asked questions on our forum. And one of them is how to customize a template without changing the standard tpl. file. In this article we will explain in details the mechanism of loading templates in Prestashop. To change a module template in your theme you need to create a folder with the module name. This method works for css, tpl and js files. For instance, if you need to change the module file blockcart.tpl without making changes into the source module, you can simply copy the file into this folder: themes/your_theme/modules/blockcart/blockcart.tpl. where your_theme – is your theme name. This method can help you avoid problems when transferring to a new version. But if you are making changes directly into the source file, then your customization can get overridden by the source file from the new version.
Such possibility gives us the function Module::display()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* ** Template management (display, overload, cache) */ protected static function _isTemplateOverloadedStatic($module_name, $template) { if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/'.$template)) //active theme file return true; elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/views/templates/hook/'.$template)) //cache file return false; elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/'.$template)) //source file return false; return null; } public function display($file, $template, $cacheId = null, $compileId = null) { if (($overloaded = Module::_isTemplateOverloadedStatic(basename($file, '.php'), $template)) === null) $result = Tools::displayError('No template found for module').' '.basename($file, '.php'); else { $this->smarty->assign(array( 'module_dir' => __PS_BASE_URI__.'modules/'.basename($file, '.php').'/', 'module_template_dir' => ($overloaded ? _THEME_DIR_ : __PS_BASE_URI__).'modules/'.basename($file, '.php').'/' //active theme file )); $smarty_subtemplate = $this->context->smarty->createTemplate( $this->getTemplatePath($template), $cacheId, $compileId, $this->smarty ); $result = $smarty_subtemplate->fetch(); } return $result; } |
Unfortunately, this method will not work with php files. This is especially inconvenient for frontend developers, who need to create new modules, which functionality for 99% matches the source modules, but that are using, for example, a different hook. But, we have managed to find a workaround how it is possible to change a hook in any standard module. To do this, we need to create a module which will be installed along with the theme. In the install function it creates an object of the required module and performs the function registerHook\unregisterHook. Let’s take the module blocknewproducts as an example, which by default has both hookLeftColumn and hookRightColumn functions but is registered only in the rightColumn hook. Let’s use the below code to move this
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private function updateOnInstall() { $return = TRUE; $blocknewproducts = Module::getInstanceByName('blocknewproducts'); $return &= $blocknewproducts->registerHook('leftColumn'); return $return; } |
Here you can download the ready-to-use example of the module.