You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PrestaShop(v8)自定义模块TPL文件HTML编码异常求助

PrestaShop 8自定义模块TPL内容HTML编码异常问题分析与解决

问题原因

核心问题出在模块的hookDisplayProductAdditionalInfo方法中:你通过$this->display()渲染了custom_instructions.tpl,并将渲染后的HTML代码赋值给Smarty变量custom_instructions。而Smarty的默认行为会对变量输出进行HTML转义,将<、"等特殊字符转换为&lt;、&quot;等实体编码,最终导致前端显示的是转义后的文本,而非可正常渲染的HTML元素。

解决建议

提供两种可行方案,推荐第二种更符合PrestaShop开发规范的写法:

方案1:取消Smarty变量的HTML转义

在product_additional_info.tpl中输出变量时,添加nofilter修饰符,强制Smarty跳过转义处理:

<div class="product-additional-info">
    {$custom_instructions nofilter}
</div>

方案2:重构代码,使用模板引入(推荐)

避免嵌套渲染HTML到变量的写法,直接在父模板中通过{include}引入子模板,Smarty会直接解析渲染,不会触发不必要的转义:

  1. 修改PHP钩子方法:
public function hookDisplayProductAdditionalInfo($params) {
    // 移除变量赋值逻辑,直接返回父模板
    return $this->display(__FILE__, 'views/templates/hook/product_additional_info.tpl');
}
  1. 修改product_additional_info.tpl模板:
<div class="product-additional-info">
    {include file='module:custominstructions/views/templates/hook/custom_instructions.tpl'}
</div>

验证说明

修改后清除PrestaShop缓存(后台→高级参数→性能→清除缓存),刷新前端页面即可看到正常渲染的label和textarea元素。

内容的提问来源于stack exchange,提问作者Rick Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 21:37:51