如何在 smarty .tpl 文件中调用 php 函数? [英] How to call a php function inside a smarty .tpl file?

查看:121
本文介绍了如何在 smarty .tpl 文件中调用 php 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .php 文件中编写了一个函数.即

Hi i have written a function in .php file. i.e.

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} 方式,但这不起作用.怎么称呼这个?

and i want to call this function inside a .tpl file. I used {php} {/php} way,but this not works. What is the way to call this one?

谢谢

推荐答案

Smarty 是一种模板语言——如果你想输出一个函数的结果,将输出分配给一个 smarty 变量

Smarty is a templating language - if you want to output the results of a function, assign the output to a smarty variable

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

然后在你的模板中使用它

And then use it in your template

{$categories}

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

There are very few situations where this isn't the correct pattern.

这篇关于如何在 smarty .tpl 文件中调用 php 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆