5

嗨,我在 .php 文件中编写了一个函数。IE

public static function getCategories($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '',$sql_limit = '')
{
    if (!Validate::isBool($active))
        die(Tools::displayError());

    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
        SELECT *
        FROM `'._DB_PREFIX_.'category` c
        LEFT JOIN `'._DB_PREFIX_.'category_lang` cl 
        ON c.`id_category` = cl.`id_category`
        WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)($id_lang) : '').'
        '.($active ? 'AND `active` = 1' : '').'
        '.(!$id_lang ? 'GROUP BY c.id_category' : '').'
        '.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, c.`position` ASC').'
        '.($sql_limit != '' ? $sql_limit : '')
    );

    if (!$order)
        return $result;

    $categories = array();
    foreach ($result AS $row)
    {
        $categories[$row['id_parent']][$row['id_category']]['infos'] = $row;
    }
    return $categories;
}

我想在 .tpl 文件中调用这个函数。我使用{php} {/php}了方式,但这不起作用。怎么称呼这个?

谢谢

4

4 回答 4

6

Smarty 是一种模板语言 - 如果要输出函数的结果,请将输出分配给 smarty 变量

$smarty->assign('categories', getCategories($args));

然后在您的模板中使用它

{$categories}

在极少数情况下,这不是正确的模式。

于 2011-12-28T15:31:41.800 回答
5

你可以用这样的东西,我一直用它

{category.id|function_name}

让我解释一下:

category.id = 是你想在函数中获取信息的类别的id

function_name = 是函数的名称

于 2013-09-28T12:44:20.040 回答
2

因为您使用的是静态方法,所以您将无法在函数中使用 $this 关键字。因此,您将无法使用

$this->context->smarty->assign('categories', self::getCategories($args));

因此,您应该创建一个变量并将上下文分配给它,在您的情况下,您只需要它的 smarty 部分。

$smarty = Context::getContext()->smarty;

现在您可以按如下方式使用它:

$smarty->assign('categories', self::getCategories($args));

我假设您将在同一个类中调用该函数,因此我使用了self::getCategories($args),如果您想在另一个类中使用它,请使用className::getCategories($args)

希望这对您有所帮助,尝试一下并给我反馈!;)

于 2014-09-10T20:37:13.607 回答
0

我同意亚当的观点,尽管由于给定的架构,我在 smarty 中使用 php。
还有另一种方法:使用{insert}- http://www.smarty.net/docs/en/language.function.insert.tpl

于 2011-12-28T15:43:29.637 回答