如何修复PrestaShop 1.7.8.8的「尝试访问null类型值数组偏移」提示
PrestaShop 1.7.8.8 + Bos Nature主题模板异常修复方案
问题概述
category-header.tpl模板抛出Notice: Trying to access array offset on value of type null错误- 使用
{/if}(无空格)时报错,改成{ /if }则前台直接显示模板代码
解决方案
一、修复空值访问错误
错误根源是直接访问了可能为null的数组/对象属性,需在判断前增加空值校验:
修改后的category-header.tpl代码:
<div id="js-product-list-header"> {if isset($listing.pagination) && $listing.pagination.items_shown_from == 1} <div class="block-category card card-block"> <h1 class="h1">{$category.name}</h1> <div class="block-category-inner"> {if !empty($category.description)} <div id="category-description" class="text-muted">{$category.description nofilter}</div> {/if} {if isset($category.image) && isset($category.image.large) && !empty($category.image.large.url)} <div class="category-cover"> <img src="{$category.image.large.url}" alt=" {if !empty($category.image.legend)} {$category.image.legend} {else} {$category.name} {/if}"> </div> {/if} </div> </div> {/if} </div>
关键修改点:
- 给
$listing.pagination增加isset()校验,避免null时访问子属性 - 对
$category.image.large.url做多层非空校验,确保每一级属性都存在 - 将
{if $category.description}替换为{if !empty($category.description)},提升判断严谨性
二、修复模板语法显示问题
Smarty模板对标签格式有严格要求:
- 必须使用标准格式
{/if}(无空格),带空格的{ /if }会被识别为普通文本,无法解析 - 之前无空格时报错是因为空值访问错误引发的连锁问题,修复空值校验后,使用标准
{/if}即可正常解析
额外排查步骤
- 清除PrestaShop缓存:后台进入「高级参数」→「性能」,点击「清除缓存」和「清除Smarty缓存」
- 检查主题继承:确认Bos Nature主题的
category-header.tpl是否正确继承核心模板,避免路径或语法冲突
内容的提问来源于stack exchange,提问作者Arturo Vasquez
相关产品推荐
相关产品推荐

