如何在不使用header hook的情况下从模块添加JS和CSS标记

时间:2014-02-04 09:38:43

标签: javascript css module tags prestashop

有没有办法只为包含模块的页面添加JS和CSS标签?

如果我使用代码段

$this->context->controller->addjs('js/file.js', 'all');

在“ hookHeader ”中,为使用钩子的整个网站(索引,cms页面等)添加了脚本。

谢谢,任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

例如,如果您只想在页面产品和类别中添加js文件,可以执行以下操作:

public function hookDisplayHeader($params)
{
    $allowed_controllers = array('product', 'category');
    $_controller = $this->context->controller;
    if (isset($_controller->php_self) && in_array($_controller->php_self, $allowed_controllers)) {
        $this->context->controller->addCss($this->_path . 'css/front.css', 'all');
        $this->context->controller->addJs($this->_path . 'js/front.js');
    }

}

答案 1 :(得分:1)

是的,你可以轻松地做到。 你需要做什么才能在所需的钩子函数中调用addJs或addCss。例如,如果您仅在某些页面上的左栏挂钩模块,那么如果您打电话

$this->context->controller->addjs('js/file.js', 'all');

在您的hookLeftColumn(现在的方法名称是什么:P),然后js或css文件将仅在显示该模块时使用。

或者你可以直接将css / js文件放在模板中。

答案 2 :(得分:1)

如果您的模块将出现在特定页面上,您可以定义“例外” - 可以通过后台定义>模块>职位>移植模块。

这样,该模块的hookDisplayHeader将不会被执行,也不会添加任何CSS和JS文件。从性能角度来看,这是一个更好的解决方案。