1

我正在尝试为 Prestashop 制作模块。但是我的 tpl 文件看不到变量。

支付图标.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;
}
4

1 回答 1

1

我不知道 prestashop 但我可以告诉你 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');
}

更新

我的坏: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');
}
于 2013-04-14T09:12:12.340 回答