在 AdminController 中包含 tpl 的最佳 prestashop 方式是什么?
What is the best prestashop-way to include tpl in AdminController?
我需要与我的 adminController class 中的 .tpl 文件进行交互,但是当我尝试这样做时,会出现此错误
Fatal error: Call to undefined method RiddlePageController::getCacheId() in /home/USER/public_html/prestashop/modules/RiddleModule/controllers/admin/RiddlePage.php on line 48
这是我的管理控制器代码:
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}
用于测试。
也许我做错了,这不是在我自己的管理页面中制作的最佳方式。
AdminController
class 没有实现您用来呈现 TPL 的 display
方法。
你可以在设置模块变量后使用类似这样的东西:
$this->module->display(_PS_MODULE_DIR_.$this->module->name.DIRECTORY_SEPARATOR.$this->module->name.'.php', 'content.tpl')
祝你好运。
您的错误是因为 AdminController class 没有 getCacheId
方法。
要回答您的问题,您必须进行一些小修改。
首先(扩展 ModuleAdminController
而不是 AdminController
):
class AdminRiddlePageController extends 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();
}
正如@TheDrot 告诉我们的那样,答案是使用 $this->context->smarty->fetch(location)
,但不是在 renderList 中,而是在 renderView 的 return 语句中是可以的,prestashop 获取 tpl 文件并正确加载聪明的变量。例如:
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 文件并不重要
我需要与我的 adminController class 中的 .tpl 文件进行交互,但是当我尝试这样做时,会出现此错误
Fatal error: Call to undefined method RiddlePageController::getCacheId() in /home/USER/public_html/prestashop/modules/RiddleModule/controllers/admin/RiddlePage.php on line 48
这是我的管理控制器代码:
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}
用于测试。
也许我做错了,这不是在我自己的管理页面中制作的最佳方式。
AdminController
class 没有实现您用来呈现 TPL 的 display
方法。
你可以在设置模块变量后使用类似这样的东西:
$this->module->display(_PS_MODULE_DIR_.$this->module->name.DIRECTORY_SEPARATOR.$this->module->name.'.php', 'content.tpl')
祝你好运。
您的错误是因为 AdminController class 没有 getCacheId
方法。
要回答您的问题,您必须进行一些小修改。
首先(扩展 ModuleAdminController
而不是 AdminController
):
class AdminRiddlePageController extends 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();
}
正如@TheDrot 告诉我们的那样,答案是使用 $this->context->smarty->fetch(location)
,但不是在 renderList 中,而是在 renderView 的 return 语句中是可以的,prestashop 获取 tpl 文件并正确加载聪明的变量。例如:
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 文件并不重要