Smarty: how to use PHP functions?

StackOverflowNewbie picture StackOverflowNewbie · Jan 21, 2011 · Viewed 25.5k times · Source

Say I have the following in my TPL file:

{$a}

and I want to apply certain PHP native functions (e.g. strip_tags) to that Smarty variable. Is this possible within the TPL? If so, how?

Answer

Zsolti picture Zsolti · Jan 21, 2011

You can use any php function in a smarty template in the following way:

{$a|php_function_name}

or

{$a|php_function_name:param2:param3:...}

In the second example you can specify additional parameters for the php function (the first is always $a in our case).

for example: {$a|substr:4:3} should result something like substr($_tpl_vars['a'],4,3); when smarty compiles it.