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

PrestaShop 1.7:404页面tpl无法显示分类列表求助

PrestaShop 1.7 404页面分类列表无法显示的问题解决

问题描述

我想在PrestaShop 1.7的404页面tpl文件中展示分类列表,可通过以下数组获取分类数据:

$categories = [
    ["id_category" => "720", "name" => "brakes", "link_rewrite" => "brakes", "id_shop" => "7"],
    ["id_category" => "721", "name" => "tyres", "link_rewrite" => "tyres", "id_shop" => "7"],
    ["id_category" => "722", "name" => "bumpers", "link_rewrite" => "bumpers", "id_shop" => "7"],
    ["id_category" => "723", "name" => "interiors", "link_rewrite" => "interiors", "id_shop" => "7"],
    ["id_category" => "724", "name" => "accessories", "link_rewrite" => "accessories", "id_shop" => "7"]
];

我在tpl文件中添加了以下代码,但分类名称无法显示:

<ul>
    {foreach from='$categories' item='category'}
        <li>{$category.name}</li>
    {/foreach}
</ul>

解决方法

1. 确保变量传递到模板

PrestaShop的Smarty模板无法直接访问PHP全局变量,必须在对应控制器中把$categories赋值给Smarty:

  • 打开controllers/front/ErrorController.php,找到initContent()方法
  • 添加代码将变量传递到模板:
$this->context->smarty->assign('categories', $categories);

注意要确保$categories数组已经在控制器中被正确获取。

2. 修正Smarty循环语法

你的循环中from='$categories'的单引号会导致Smarty将其识别为字符串而非变量,正确写法:

<ul>
    {foreach from=$categories item=category}
        <li>{$category.name}</li>
    {/foreach}
</ul>

3. 调试变量是否存在

在模板中添加调试代码,确认$categories是否被正确传递:

{$categories|var_dump}

如果输出为空或null,说明变量未传递到模板,需重新检查控制器的赋值逻辑。

4. 清除PrestaShop缓存

修改模板或控制器后,必须清除后台缓存(路径:高级参数→性能→清除缓存),确保修改生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:12:35