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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8657551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:12:01  来源:igfitidea点击:

How to call a php function inside a smarty .tpl file?

phpsmartyprestashop

提问by srinu

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

嗨,我在 .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;
}

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?

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

Thanks

谢谢

回答by Adam Hopkinson

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

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

$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.

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

回答by Usf Ahmed Subehi

you can use something like this and i always used it

你可以使用这样的东西,我总是用它

{category.id|function_name}

let me explain it :

让我解释一下:

category.id = is the id of the category you want to get information about in the function

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

function_name = is the name of the function

function_name = 是函数名

回答by Youssef K. Yassin

because you are using a static method, you will not be able to use $this keyword in your function. So therefore, you will not be able to use

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

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

So you should create a variable and assign the context to it and in your case you will only need the smarty part of it.

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

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

Now you can use this as follows:

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

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

I'm assuming that you will call the function in the same class, so I used self::getCategories($args), if you want to use it in another class use className::getCategories($args)

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

Hope this helps you, try it and give me feedback! ;)

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

回答by lvil

I agree with adam, though somitemies I use the php inside smarty due to given architecture.
There is another way to do it: with {insert}-http://www.smarty.net/docs/en/language.function.insert.tpl

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