Prestashop 1.6 - 在 adminController 中使用我的自定义 content.tpl
Prestashop 1.6 - use my custom content.tpl in adminController
我在我的模块中定义了我的 content.tpl 模板,所以我想在我从 prestashop AdminController 扩展的管理控制器中使用它。
我知道有一个 属性,我可以在其中设置我的模板的路径,但我不知道如何定义正确的路径,因为当我执行代码 prestashop 时添加 `
'C:\xampp\htdocs\prestashop\admin0559umpxx/themes/default\template\`
给我的 link.
我的自定义模板位于 mymodule/views/templates/admin/content.tpl,我用来设置路径的 属性 是 $this->template.
确实,我的代码是这样的:
class AdminSelStockController extends AdminController
{
public function __construct()
{
$this->module = new SelStock();
$this->addRowAction('edit'); //add an edit button
$this->addRowAction('delete'); //add a delete button
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->id_lang = $this->context->language->id;
$this->path = _MODULE_DIR_."selStock";
$this->default_form_language = $this->context->language->id;
$this->table = 'selstock_product'; //define the main table
$this->className = 'SelStockProductModel'; //define the module entity
$this->identifier = "id_selstock_product"; //the primary key
//then define select part of the query
$this->_select = 'a.id_selstock_product,a.image_path,a.reference,a.product_name,a.category_name,a.quatity';
//join to an existing table if you need some extra informations
$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'selstock_product_lang` spl ON (spl.`id_selstock_product` = a.`id_selstock_product`)';
$this->_where = "AND spl.id_lang = '".$this->id_lang."'";
$this->bootstrap = true;
$this->layout = _PS_MODULE_DIR_.'/selStock/views/templates/admin/layout.tpl';
$this->template = _PS_MODULE_DIR_.'/selStock/views/templates/admin/content.tpl';
//and define the field to display in the admin table
$this->fields_list = array(
'id_selstock_product' => array(
'title' => $this->l('Product Num'),
'align' => 'center',
'width' => 120
),
'image_path' => array(
'title' => $this->l('Image'),
'align' => 'center',
'width' => 120
),
'reference' => array(
'title' => $this->l('Reference'),
'align' => 'center',
'width' => 120
),
'product_name' => array(
'title' => $this->l('Name'),
'align' => 'center',
'width' => 120
),
'category_name' => array(
'title' => $this->l('Category'),
'align' => 'center',
'width' => 120
),
'quatity' => array(
'title' => $this->l('Quantity'),
'align' => 'center',
'width' => 120
),
'store_name' => array(
'title' => $this->l('Store'),
'align' => 'center',
'width' => 120
)
);
parent::__construct();
}
布局没问题,我可以用我的own.but我不知道模板的问题在哪里。
非常酷,我通过将这些函数添加到我的 adminController 找到了解决方案:
/**
* Get path to back office templates for the module
*
* @return string
*/
public function getTemplatePath()
{
return _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/';
}
public function createTemplate($tpl_name) {
if (file_exists($this->getTemplatePath() . $tpl_name) && $this->viewAccess())
return $this->context->smarty->createTemplate($this->getTemplatePath() . $tpl_name, $this->context->smarty);
return parent::createTemplate($tpl_name);
}
public function initContent(){
parent::initContent();
$tpl = $this->createTemplate('content.tpl')->fetch();
/* DO STUFF HERE */
// $posts = array();
// $this->context->smarty->assign('posts', $posts);
}
您应该使用 ModuleAdminController 而不是 AdminController
扩展您的 class
我在我的模块中定义了我的 content.tpl 模板,所以我想在我从 prestashop AdminController 扩展的管理控制器中使用它。
我知道有一个 属性,我可以在其中设置我的模板的路径,但我不知道如何定义正确的路径,因为当我执行代码 prestashop 时添加 `
'C:\xampp\htdocs\prestashop\admin0559umpxx/themes/default\template\`
给我的 link.
我的自定义模板位于 mymodule/views/templates/admin/content.tpl,我用来设置路径的 属性 是 $this->template.
确实,我的代码是这样的:
class AdminSelStockController extends AdminController
{
public function __construct()
{
$this->module = new SelStock();
$this->addRowAction('edit'); //add an edit button
$this->addRowAction('delete'); //add a delete button
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->id_lang = $this->context->language->id;
$this->path = _MODULE_DIR_."selStock";
$this->default_form_language = $this->context->language->id;
$this->table = 'selstock_product'; //define the main table
$this->className = 'SelStockProductModel'; //define the module entity
$this->identifier = "id_selstock_product"; //the primary key
//then define select part of the query
$this->_select = 'a.id_selstock_product,a.image_path,a.reference,a.product_name,a.category_name,a.quatity';
//join to an existing table if you need some extra informations
$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'selstock_product_lang` spl ON (spl.`id_selstock_product` = a.`id_selstock_product`)';
$this->_where = "AND spl.id_lang = '".$this->id_lang."'";
$this->bootstrap = true;
$this->layout = _PS_MODULE_DIR_.'/selStock/views/templates/admin/layout.tpl';
$this->template = _PS_MODULE_DIR_.'/selStock/views/templates/admin/content.tpl';
//and define the field to display in the admin table
$this->fields_list = array(
'id_selstock_product' => array(
'title' => $this->l('Product Num'),
'align' => 'center',
'width' => 120
),
'image_path' => array(
'title' => $this->l('Image'),
'align' => 'center',
'width' => 120
),
'reference' => array(
'title' => $this->l('Reference'),
'align' => 'center',
'width' => 120
),
'product_name' => array(
'title' => $this->l('Name'),
'align' => 'center',
'width' => 120
),
'category_name' => array(
'title' => $this->l('Category'),
'align' => 'center',
'width' => 120
),
'quatity' => array(
'title' => $this->l('Quantity'),
'align' => 'center',
'width' => 120
),
'store_name' => array(
'title' => $this->l('Store'),
'align' => 'center',
'width' => 120
)
);
parent::__construct();
}
布局没问题,我可以用我的own.but我不知道模板的问题在哪里。
非常酷,我通过将这些函数添加到我的 adminController 找到了解决方案:
/**
* Get path to back office templates for the module
*
* @return string
*/
public function getTemplatePath()
{
return _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/';
}
public function createTemplate($tpl_name) {
if (file_exists($this->getTemplatePath() . $tpl_name) && $this->viewAccess())
return $this->context->smarty->createTemplate($this->getTemplatePath() . $tpl_name, $this->context->smarty);
return parent::createTemplate($tpl_name);
}
public function initContent(){
parent::initContent();
$tpl = $this->createTemplate('content.tpl')->fetch();
/* DO STUFF HERE */
// $posts = array();
// $this->context->smarty->assign('posts', $posts);
}
您应该使用 ModuleAdminController 而不是 AdminController
扩展您的 class