【问题标题】:What is the best prestashop-way to include tpl in AdminController?在 AdminController 中包含 tpl 的最佳 prestashop 方式是什么?
【发布时间】:2017-01-20 17:28:27
【问题描述】:

我需要与我的 adminController 类中的 .tpl 文件进行交互,但是当我尝试这样做时,会出现此错误

致命错误:在第 48 行的 /home/USER/public_html/prestashop/modules/RiddleModule/controllers/admin/RiddlePage.php 中调用未定义的方法 RiddlePageController::getCacheId()

这是我的管理控制器代码:

class RiddlePageController extends AdminController {

public function __construct()
{
    $this->html = '';
    $this->display = 'view';
    $this->meta_title = $this->l('metatitle');
    $this->module = "RiddleModule";

    parent::__construct();
}

public function initContent()
{
    $this->postProcess();
    $this->show_toolbar = true;
    $this->display = 'view';
    $this->meta_title = $this->l('Modulo');
    parent::initContent();  
}

public function initToolBarTitle()
{
    $this->toolbar_title = $this->l('Titulo');
}

public function initToolBar()
{
    return true;
}

public function renderView() {
    $this->context->smarty->assign(
        array( 
            'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
            'img2' => "http://www.all3dmodel.com/Images/39.jpg"
            )
        );
    // in return have error "getCacheId"
    return $this->display(__FILE__, 'content.tpl', $this->getCacheId());
    // return "<b>This works fine!!</b>";

}

我的 tpl 文件只有 {$img1}{$img2} 用于测试。

也许我做错了,这不是在我自己的管理页面中制作的最佳方式。

【问题讨论】:

    标签: php model-view-controller module prestashop prestashop-1.6


    【解决方案1】:

    您的错误是因为 AdminController 类没有 getCacheId 方法。

    要回答您的问题,您必须进行一些小修复。

    首先(扩展 ModuleAdminController 而不是 AdminController):

    类 AdminRiddlePageController 扩展 ModuleAdminController { }

    然后,如果您想查看自定义 tpl,请将 view.tpl 文件放入:
    prestashop/modules/RiddleModule/views/templates/admin/riddlepage/helpers/view/

    prestashop/modules/RiddleModule/views/templates/admin/riddle_page/helpers/view/
    (我不记得是否需要下划线)

    而你的renderView 方法应该是这样的:

    public function renderView()
    {
        /* Your code */
    
        /* Use this snippet to assign vars to smarty */
        $this->tpl_view_vars = array(
            'myvar' => 1,
            'secondvar' => true
        )
        return parent::renderView();
    }
    

    【讨论】:

    • 它不起作用我的朋友...但好消息是您的解决方案现在只显示空白页,没有错误...让我告诉你prnt.sc/dy8bp5
    • @manhattan This 应该可以帮助您了解如何在管理控制器中加载自定义 tpl。
    • @manhattan 我已经做出了改变,试试AdminRiddlePageController而不是RiddlePageController,别忘了更新DB中的tab
    • @sarcom 是的,当控制器名称是驼峰式的多个单词时,下划线是必需的。所以riddle_page 是正确的文件夹。
    • @TheDrot 谢谢!!你的建议对我有帮助……但有一点改变;在渲染视图中,我返回了 $this->context->smarty->fetch(ANYWHERE);并将文件加载到任何位置,并支持 smarty 变量!
    【解决方案2】:

    AdminController 类没有实现您用来呈现 TPL 的 display 方法。

    你可以在 set module var 之后使用这样的东西:

    $this->module->display(_PS_MODULE_DIR_.$this->module->name.DIRECTORY_SEPARATOR.$this->module->name.'.php', 'content.tpl')
    

    祝你好运。

    【讨论】:

      【解决方案3】:

      正如@TheDrot 告诉我们的那样,答案是使用$this-&gt;context-&gt;smarty-&gt;fetch(location),但不是在renderList 中,而是在renderView 的return 语句中可以,并且prestashop 获取tpl 文件并正确加载smarty 变量。例如:

      public function renderView(){
          $this->context->smarty->assign(
              array( 
                  'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
                  'img2' => "http://www.all3dmodel.com/Images/39.jpg"
                  )
              );
      
          return $this->context->smarty->fetch(_PS_MODULE_DIR_ . "RiddleModule/controllers/front/prueba.tpl");
      }
      

      在这种情况下,文件位置对于加载 TPL 文件并不重要

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 2016-03-22
        相关资源
        最近更新 更多