嗨,我已经在。php文件中编写了一个函数。例如
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}
的方式,但这不起作用。这个怎么叫呢?
谢谢
Smarty是一种模板语言——如果你想输出一个函数的结果,把输出赋值给Smarty变量
$smarty->assign('categories', getCategories($args));
然后在你的模板中使用
{$categories}
在极少数情况下,这不是正确的模式。
你可以这样写我一直都这么用
{category.id|function_name}
让我解释一下:
类别。Id =是要在
函数中获取信息的类别的Id。function_name =函数名
因为使用的是静态方法,所以不能在函数中使用$this关键字。因此,您将不能使用
$this->context->smarty->assign('categories', self::getCategories($args));
所以你应该创建一个变量,并将上下文赋值给它,在你的情况下,你只需要它的智能部分。
$smarty = Context::getContext()->smarty;
现在你可以这样使用:
$smarty->assign('categories', self::getCategories($args));
我假设您将在同一个类中调用函数,所以我使用self::getCategories($args),如果您想在另一个类中使用它,请使用className::getCategories($args)
希望这对你有帮助,尝试一下,给我反馈!;)
我同意adam的观点,尽管有时由于给定的架构,我在smarty内部使用php。
还有另一种方法:使用{insert}
-http://www.smarty.net/docs/en/language.function.insert.tpl