JulienJacob Posted May 15 Share Posted May 15 Hello , i have a custom module with front controller , its tpl file includes a hook as below : {block name='faq'} {hook h='DisplayFaqWrapper' mod='PsCustom'} {/block} Below , is my hook , I used to manage cache but it doesnt work : <?php namespace PrestaShop\Module\PsCustom\Hook; use Cache; use Context; use PsCustom; use Doctrine\ORM\EntityManagerInterface; use Exception; use PrestaShop\Module\PsCustom\Entity\PsCustomCategory; use PrestaShop\Module\PsCustom\Service\PsCustomCategoryService; use Tools; /** * Trait DisplayFaqHook * @package PrestaShop\Module\PsCustom\Hook */ trait DisplayFaqHook { /** @var string */ public $template = 'module:' . PsCustom::MODULE_NAME . '/views/templates/hook/faq.tpl'; /** @var string */ public $faqCacheId = PsCustom::MODULE_NAME . '|hookDisplayFaqWrapper'; /** * Render faq template from cache * @throws Exception */ public function hookDisplayFaqWrapper(): string { try { /** @var PsCustomCategoryService $faqService */ $faqService = $this->get('prestashop.module.ps_custom.service.ps_custom_category'); //if (Tools::isSubmit('submitFaq')) { $searchFaq = htmlspecialchars_decode(trim(Tools::getValue('searchFaq'))); $data['searchFaq'] = $searchFaq; //} // var_dump($this->getCacheId('PsCustom')); // var_dump($this->isCached($this->template)); // var_dump($this->isCached($this->template, $this->getCacheId($this->faqCacheId))); if (! $this->isCached($this->template, $this->getCacheId($this->faqCacheId))) { // die('block 1'); $data['PsCustomCategories'] = $faqService->getFaqByCategories($searchFaq); $this->context->smarty->assign('data', $data); } else { die('block 2'); } if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) { $this->context->smarty->assign('data', $data); return $this->fetch($this->template); } //die('not in cache block'); return $this->fetch($this->template, $this->getCacheId($this->faqCacheId)); } catch (Exception $exception) { error_log('CPM LOG: ' . __METHOD__ . ' ' . $exception->getMessage() . ' ' . $exception->getTraceAsString()); return false; } } } This instruction always return false : $this->isCached($this->template, $this->getCacheId($this->faqCacheId)) Any idea to fix the cache ? Thanks in advance Link to comment Share on other sites More sharing options...
Prestashop Addict Posted May 16 Share Posted May 16 (edited) Did you implement WidgetInterface? Edited May 16 by Prestashop Addict (see edit history) Link to comment Share on other sites More sharing options...
JulienJacob Posted May 16 Author Share Posted May 16 not it is a sample module Link to comment Share on other sites More sharing options...
Prestashop Addict Posted May 16 Share Posted May 16 You have multiple $this->fetch( and one of them doesn't have a cache id Link to comment Share on other sites More sharing options...
JulienJacob Posted May 16 Author Share Posted May 16 in this filter context, I don't want to cache the search result, otherwise it will be shared with all users, so I've put in 2 templates if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) { $this->context->smarty->assign('data', $data); return $this->fetch($this->template); } Link to comment Share on other sites More sharing options...
JulienJacob Posted May 17 Author Share Posted May 17 (edited) is there a prestashop module with front controller that uses cache? Edited May 17 by JulienJacob (see edit history) Link to comment Share on other sites More sharing options...
Prestashop Addict Posted May 17 Share Posted May 17 il y a 16 minutes, JulienJacob a dit : is there a prestashop module with front controller that uses cache? It seems that your module is not a ModuleFrontController, but a module with a custom hook. You should add $params to your custom hook public function hookDisplayFaqWrapper($param): string Do you make a $this->registerHook('displayFaqWrapper'); in your install() function? Try $cacheId = $this->getCacheId($this->faqCacheId); if (! $this->isCached('faq.tpl', $cacheId)) { and return $this->display(__FILE__, 'faq.tpl', $cacheId); Link to comment Share on other sites More sharing options...
JulienJacob Posted May 17 Author Share Posted May 17 8 minutes ago, Prestashop Addict said: It seems that your module is not a ModuleFrontController, but a module with a custom hook. You should add $params to your custom hook public function hookDisplayFaqWrapper($param): string Do you make a $this->registerHook('displayFaqWrapper'); in your install() function? Try $cacheId = $this->getCacheId($this->faqCacheId); if (! $this->isCached('faq.tpl', $cacheId)) { and return $this->display(__FILE__, 'faq.tpl', $cacheId); return $this->display(__FILE__, 'faq.tpl', $cacheId); makes an error : no template found Yes my custom module have a front controller , wich includes a custom hook <?php /** * Class PsCustomDisplayFaqModuleFrontController */ class PsCustomDisplayFaqModuleFrontController extends ModuleFrontController { /** @var string */ public $templateFaq = 'module:' . PsCustom::MODULE_NAME . '/views/templates/front/wrapper.tpl'; public function __construct() { parent::__construct(); } /** * @see FrontController::initContent() */ public function initContent() { parent::initContent(); $this->setTemplate($this->templateFaq); } /** * Method called by default to add specify js and css for the view called by the initContent function */ public function setMedia(): void { parent::setMedia(); $this->registerStylesheet( 'module-PsCustom-display-faq', 'modules/' . $this->module->name . '/views/css/display-faq.css', [ 'media' => 'all', 'priority' => 200, ] ); $this->registerJavascript( 'module-PsCustom-display-faq', 'modules/' . $this->module->name . '/views/js/display-faq.js', [ 'media' => 'all', 'priority' => 200, ] ); } } here the faq.tpl : {block name='faq'} {hook h='DisplayFaqWrapper' mod='cpmfaq'} {/block} and the hook code as below <?php namespace PrestaShop\Module\PsCustom\Hook; use Cache; use Context; use PsCustom; use Doctrine\ORM\EntityManagerInterface; use Exception; use PrestaShop\Module\PsCustom\Entity\PsCustomCategory; use PrestaShop\Module\PsCustom\Service\PsCustomCategoryService; use Tools; /** * Trait DisplayFaqHook * @package PrestaShop\Module\PsCustom\Hook */ trait DisplayFaqHook { /** @var string */ public $template = 'module:' . PsCustom::MODULE_NAME . '/views/templates/hook/faq.tpl'; /** @var string */ public $faqCacheId = PsCustom::MODULE_NAME . '|hookDisplayFaqWrapper'; /** * Render faq template from cache * @throws Exception */ public function hookDisplayFaqWrapper(): string { try { /** @var PsCustomCategoryService $faqService */ $faqService = $this->get('prestashop.module.ps_custom.service.ps_custom_category'); //if (Tools::isSubmit('submitFaq')) { $searchFaq = htmlspecialchars_decode(trim(Tools::getValue('searchFaq'))); $data['searchFaq'] = $searchFaq; //} // var_dump($this->getCacheId('PsCustom')); // var_dump($this->isCached($this->template)); // var_dump($this->isCached($this->template, $this->getCacheId($this->faqCacheId))); if (! $this->isCached($this->template, $this->getCacheId($this->faqCacheId))) { // die('block 1'); $data['PsCustomCategories'] = $faqService->getFaqByCategories($searchFaq); $this->context->smarty->assign('data', $data); } else { die('block 2'); } if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) { $this->context->smarty->assign('data', $data); return $this->fetch($this->template); } //die('not in cache block'); return $this->fetch($this->template, $this->getCacheId($this->faqCacheId)); } catch (Exception $exception) { error_log('CPM LOG: ' . __METHOD__ . ' ' . $exception->getMessage() . ' ' . $exception->getTraceAsString()); return false; } } } Link to comment Share on other sites More sharing options...
Prestashop Addict Posted July 5 Share Posted July 5 Le 17/05/2024 à 11:06 AM, JulienJacob a dit : is there a prestashop module with front controller that uses cache? Tested on 1.7.8.12 In your module implement theses functions public function isTemplateCached($template) { return $this->isCached($template, $this->getCacheId()); } public function myrenderTemplate($template, array $vars) { if (!$this->isCached($template, $this->getCacheId())) { $this->smarty->assign($vars); } return $this->fetch($template, $this->getCacheId()); } And in your moduleFrontController $template = 'module:modulename/views/template/hook/mytemplate.tpl'; $tplVars = []; if (!$this->module->isTemplateCached($template)) { // Fill your tplVars[] ... } $html = $this->module->myrenderTemplate($template, $tplVars); And if you want to clear the templact cache yourself in the module parent::_clearCache('module:modulename/views/template/hook/mytemplate.tpl'); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now