如何添加<;脚本>;标记到模块文件中的prestashop页面中


how to add <script> tag into prestashop page in module file

我使用Prestashop的hookAuthentication,我想在这个Hook中运行一个脚本(用户登录时只有一个):

公共函数hookAuthentication(){}

和脚本:

var data={contact_id:5,title:'Mr',gender:1,first_name:'Upont',last_name:'Pierre'}

那我该怎么办呢?,请帮帮我,非常感谢!

在您可以编写的函数末尾的目录"views/templates/front/"中创建一个文件mymodule.tpl:

return $this->display(__FILE__, 'views/templates/front/mymodules.tpl');

在mymodule.tpl中输入:

{literal}
<script>
var data ={ contact_id:5, title:'Mr', gender:1, first_name:'Dupont', last_name:'Pierre'};
</script>
{/literal}

您可以将脚本保存在一个文件中,比如mymodule/js/myscript.js,然后

public function hookAuthentication() {
  $this->context->controller->addJS(_MODULE_DIR_ . 'mymodule/js/myscript.js');
}

通过这种方式,您将在生成的页面中加载任何脚本。