Smarty:是否可以在模板内调用PHP函数(从controller类)? [英] Smarty: is it possible to call a PHP function (from the controller class) inside the template?
本文介绍了Smarty:是否可以在模板内调用PHP函数(从controller类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在控制器类中创建了一个需要在模板内部使用的函数(因为该模板已多次渲染(每个模板是网站上的一则帖子,并且它们都将在Newsfeed中显示为不同的值)有没有办法在一个聪明的变量上调用它?假设您在控制器中创建了一个函数:
I created a function in the controller class that needs to be used inside the template (because the template it rendered many times (each template is a post on the website, and they will be all displayed with different values in the newsfeed). Is there a way to call it on a smarty variable? Say you created a function in the controller:
public function foo($bar){
$bar++;
return $bar;
}
然后在模板中:
{$ smarbar | foo}
或类似的
推荐答案
您可以注册一个修饰符,该修饰符将调用类的给定方法:
You could register a modifier that will call a given method of a class:
class SomeClass {
public function someMethod($value)
{
// return modified value
}
}
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'myModifier', array('SomeClass', 'someMethod'));
// or
$instance = new SomeClass();
$smarty->registerPlugin('modifier', 'myModifier', array($instance, 'someMethod'));
现在您可以在模板中使用它:
and now you can use this inside your template:
{$someVar|myModifier}
这是 registerPlugin 文档.
这篇关于Smarty:是否可以在模板内调用PHP函数(从controller类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文