我正试图为prestshop制作模块。但是我的tpl文件看不到变量。
payicon.php:
function hookFooter($params){
$group_id="{$base_dir}modules/mymodule/payicon.php";
$smarty = new Smarty;
$smarty->assign('group_id', '$group_id');
return $this->display(__FILE__, 'payicon.tpl');
return false;
}
payicon.tpl:
<div id="payicon_block_footer" class="block">
<h4>Welcome!</h4>
<div class="block_content">
<ul>
<li><a href="{$group_id}" title="Click this link">Click me!</a></li>
</ul>
</div>
</div>
更新:
这是安装:
public function install() {
if (!parent::install() OR !$this->registerHook('Footer'))
return false;
return Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'pay_icon` (
`id_icon` int(10) unsigned NOT NULL,
`icon_status` varchar(255) NOT NULL,
`icon_img` varchar(255) DEFAULT NULL,
`icon_link` varchar(255) NOT NULL,
PRIMARY KEY (`id_icon`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
return true;
}
我不知道prestshop,但我可以告诉你关于smarty和PHP。我可以在代码
中看到许多明显的问题。 1) $base_dir
在函数中不可用。添加
global $base_dir;
放在函数的开头,使其在函数的作用域中可用。
2)
$smarty = new Smarty;
我认为这一行不应该在那里。这是初始化一个新的Smarty
实例,它与函数外的代码无关。
将这一行替换为
global $smarty;
将使全局$smarty
(Smarty
类的实例)在此函数中可用
3)
$smarty->assign('group_id', '$group_id');
是错误的。用
代替$smarty->assign('group_id', $group_id);
解决方案可能
由于你的问题没有得到太多的关注,我将尽力想出一个答案,至少,让你在正确的方向(如果不能解决你的问题)
尝试用
替换此函数public function hookFooter($params){
global $base_dir;
global $smarty;
$group_id="{$base_dir}modules/mymodule/payicon.php";
$smarty->assign('group_id', '$group_id');
return $this->display(__FILE__, 'payicon.tpl');
}
My bad:D。忘记在最终代码中替换'$group_id'
。试试这个
public function hookFooter($params){
global $base_dir;
global $smarty;
$group_id="{$base_dir}modules/mymodule/payicon.php";
$smarty->assign('group_id', $group_id);
return $this->display(__FILE__, 'payicon.tpl');
}