假设我的TPL文件中包含以下内容:
{$a}
我想将某些PHP本机函数(例如strip_tags)应用于该Smarty变量.这可能在TPL中吗?如果是这样,怎么样?
Zso*_*lti 17
您可以通过以下方式在智能模板中使用任何php函数:
{$a|php_function_name}
Run Code Online (Sandbox Code Playgroud)
要么
{$a|php_function_name:param2:param3:...}
Run Code Online (Sandbox Code Playgroud)
在第二个例子中,您可以为php函数指定其他参数(在我们的例子中,第一个参数总是$ a).
例如:
{$a|substr:4:3}应该产生像substr($_tpl_vars['a'],4,3);smarty编译它的东西.
非常好的问题,我花了一段时间才完全弄明白这一点.
调用一个函数,传递一个参数:
{"this is my string"|strtoupper}
// same as:
strtoupper("this is my string")
{$a:strtoupper}
// same as:
strtoupper($a)
Run Code Online (Sandbox Code Playgroud)
调用函数,传递多个参数
{"/"|str_replace:"-":"this is my string"}
// same as:
str_replace("/", "-", "this is my string")
{"/"|str_replace:"-":$a}
// same as:
str_replace("/", "-", $a)
Run Code Online (Sandbox Code Playgroud)
最好的方法可能是为Smarty 创建自己的插件和修饰符.对于您的具体示例,Smarty已经有一个strip_tags修饰符.像这样使用它:
{$a|strip_tags}
Run Code Online (Sandbox Code Playgroud)