如何从prestashop中的自定义模块中的管理控制器加载模板文件

时间:2016-06-15 09:51:23

标签: prestashop prestashop-1.6

如何从prestashop 1.6中的自定义模块中的管理控制器加载模板文件

if (!defined('_PS_VERSION_')) exit;
 class QueryAllTrxController extends ModuleAdminController
{

public $module;
public function __construct()
{
parent::__construct();
}

public function initContent()
{
parent::initContent();
$this->setTemplate('display.tpl');
//$this->setTemplate(_PS_THEME_DIR_.'mypage.tpl');
}    

}

5 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并且需要永远弄明白。 我最终在此视频中发现了解决方案:https://www.youtube.com/watch?v=CdnJpLqqvcM

这就是我开始工作的方式:

1 - 在ModuleName / controllers / AdminMyControllerNameController.php

中创建控制器
class AdminMyControllerNameController extends ModuleAdminController
{

public function __construct()
{
    $this->display = 'view';
    $this->meta_title = $this->l('metatitle');
    $this->toolbar_title = $this->l('tollbartitle');
    parent::__construct();
}

public function initContent()
{
    $this->show_toolbar = true;
    $this->display = 'view';
    $this->meta_title = $this->l('META TITLE');

    parent::initContent();

    $this->setTemplate('templatename.tpl');


}

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

public function initToolBar()
{
    return true;
}

}

2 - 在ModuleName / views / admin / my_controller_name / template.tpl中创建模板文件

你必须使用用下划线写的控制器名称在views / admin文件夹中创建一个目录。

无论如何,我希望这会有所帮助。

答案 1 :(得分:0)

代码<Location>正在加载模板文件$this->setTemplate('display.tpl');modules/your-custom-module/views/templates/admin/display.tpl

答案 2 :(得分:0)

必须以这样的方式命名类名:AdminQueryAllTrxController

你可以将display.tpl放入:

modules\module_name\views\templates\admin\classe_name(QueryAllTrx)

并在AdminQueryAllTrxController中使用:$this->setTemplate('display.tpl');

答案 3 :(得分:0)

首先将控制器添加到模块:

modules\module_name\controllers\admin\SomeNameController.php

并通过ModuleAdminController对其进行扩展,您至少需要两种方法才能使其正常工作__construct和initContent 将以下代码添加到后面的方法中:

$this->content .= $this->context->smarty->fetch($this->pathToTpl);

            $this->context->smarty->assign(array(
                'content' => $this->content,
            ));

您可以将$ this-> pathToTpl替换为指向您的tpl文件的任何路径,我更愿意动态创建路径。您可以在此处看到一个简单的示例:

class SomeNameController extends ModuleAdminController{
  var $pathToTpl;
  public function __construct()
    {
        $this->bootstrap = true;
        $this->context = Context::getContext();

        $this->pathToTpl = _PS_MODULE_DIR_ .
            $this->module->name .  // put the name of module
            '/views/templates/admin' .
            '/' .
            'templateName.tpl';
        parent::__construct();
    }

  public function initContent()
    {
        parent::initContent();

        $this->content .= $this->context->smarty->fetch($this->pathToTpl);

        $this->context->smarty->assign(array(
            'content' => $this->content,
        ));

    }

}

最后,您需要将templateName.tpl放置在您想要的路径中:

modules\module_name\views\templates\admin\templateName.tpl

答案 4 :(得分:0)

此处工作代码

背景

您要添加带有自定义模块控制器的自定义管理页面。但是您无法自定义模板,因为您会遇到以下错误消息:

致命错误:没捉到->聪明:无法加载模板文件'/var/www/html/admin-dev/themes/default/template/catalog/index.tpl'<-抛出于/ var / www /html/tools/smarty/sysplugins/smarty_internal_templatebase.php在第129行

您当前的源代码是:

class AdminYourModuleNameProductsController extends ModuleAdminController {

    public function initContent() {
        parent::initContent();
        // enable these lines if you're stuck with damn stupid blank page with just 500 error
        // ini_set('display_errors', '1');
        // ini_set('display_startup_errors', '1');
        // error_reporting(E_ALL);
        $this->setTemplate('products/index.tpl');
    }

}

您不知道要怎么做,因为PrestaShop开发文档是电子商务平台开发人员文档历史上最糟糕的文档,而且其论坛上充斥着各种讨论。

解决方案

index.tpl放在

{%PRESTA_ROOT%} / modules / {%YOUR MODULE DIR%} / views / templates / admin / {%蛇壳版本的控制器%} / products / index.tpl

例如,如果您的模块名称为yourmodulename,而控制器名称为AdminYourModuleNameProductsController(如示例所示),则正确的路径为:

{%PRESTA_ROOT%} / modules / yourmodulename / views / templates / admin / your_module_name_products / products / index.tpl

如果错误仍然存​​在:

选中此line

{%PRESTA_ROOT%} / classes / controller / ModuleAdminController.php

public function createTemplate($tpl_name)
{
    if (file_exists(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name) && $this->viewAccess()) {
        // echo the following line and exit
        return $this->context->smarty->createTemplate(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name, $this->context->smarty);
    } elseif (file_exists($this->getTemplatePath().$this->override_folder.$tpl_name) && $this->viewAccess()) {
        // echo the following line and exit
        return $this->context->smarty->createTemplate($this->getTemplatePath().$this->override_folder.$tpl_name, $this->context->smarty);
    }
    // the error occurs because php get reach to the following line:
    return parent::createTemplate($tpl_name);
}

按照我的评论进行操作,您可以获得正确的文件路径。确保文件存在于路径中。

我的PrestaShop版本是1.6.1.24

相关问题