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

PrestaShop模块后台配置页如何仅加载Smarty模板内容

解决PrestaShop模块后台仅加载自定义Smarty模板内容的问题

你的代码存在核心逻辑问题,同时需要调整渲染规则来绕过后台框架,具体修复方案如下:

1. 修复模板渲染结果的返回问题

原代码中displayMyPage方法没有返回Smarty渲染后的内容,导致getContent无法获取到输出内容,这是最基础的错误。

2. 正确启用仅内容模式并优化渲染流程

$this->content_only = true需要在触发自定义动作的分支中设置,部分版本的PrestaShop中仅靠这个属性可能无法完全跳过框架渲染,此时可以直接输出模板内容并终止脚本。

修改后的完整代码

class MyModule extends Module
{
    public function getContent()
    {
        if (Tools::getValue('customAction') === 'displayMyPage') 
        {
            // 开启仅内容模式,告知模块不包裹后台框架
            $this->content_only = true;
            return $this->displayMyPage();
        }

        $url = $this->context->link->getAdminLink('AdminModules', true, 
        array('configure' => $this->name, 'customAction' => 'displayMyPage'));

        return '<a href="'.$url.'" class="btn btn-danger">Open MyPage</a>';
    }

    public function displayMyPage() 
    {
        $templateURI = __DIR__.'/views/templates/admin/myPage.tpl';
        // 返回渲染后的模板内容
        return $this->context->smarty->fetch($templateURI);
    }
}

若上述方案仍不生效(针对部分PrestaShop版本)

可以直接输出模板内容并终止脚本,彻底跳过后台框架的后续渲染流程:

public function displayMyPage() 
{
    $templateURI = __DIR__.'/views/templates/admin/myPage.tpl';
    $output = $this->context->smarty->fetch($templateURI);
    
    // 直接输出内容并终止脚本
    echo $output;
    exit;
}

额外检查点

  • 确认myPage.tpl文件存在于模块目录的views/templates/admin/路径下
  • 确保模板文件没有语法错误,避免渲染失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:03:14