php - 如何在 smarty .tpl 文件中调用 php 函数?

标签 php smarty prestashop

您好,我在 .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}

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

关于php - 如何在 smarty .tpl 文件中调用 php 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657551/

相关文章:

prestashop - 如何在 prestashop 中更改订单确认电子邮件模板的内容?

javascript - 获取当前 URL 未获取 ID、基于类的过滤器

php - 如何使用简单的 html dom 打印表格的单元格

php - WebView 中的按钮链接回另一个应用程序 Activity

php - 在 PHP 中扩展类方法

symfony - 如何让 Smarty3 与 Symfony2 一起工作?

php - 如何从 smarty 模板中的 URL 访问变量值?

smarty - 使用 smarty 进行 URL 编码

web-services - Web 服务添加/更新组合 Prestashop

php - 使用 db 类更新 Prestashop 数据库