10

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?

0

6 Answers 6

26

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.

Sign up to request clarification or add additional context in comments.

1 Comment

{$a|php_function_name:param1:param3:...} should be {$a|php_function_name:param2:param3:...}
10

Or you can use this: (call function directly)

{rand()}

2 Comments

This is really nice option and deserves more notice.
Further more you can not only call that way a native php functions but also functions you declared in your php code without registering it as plugin (tested for Smarty 3).
9

The best way is probably to create your own plugins and modifiers for Smarty. For your specific example, Smarty already has a strip_tags modifier. Use it like this:

{$a|strip_tags}

Comments

8

Very good question, it took me a while to completely figure this one out.

Call a function, passing a single parameter:

{"this is my string"|strtoupper}
// same as:
strtoupper("this is my string")

{$a:strtoupper}
// same as:
strtoupper($a)

Call a function, passing multiple parameters

{"/"|str_replace:"-":"this is my string"}
// same as:
str_replace("/", "-", "this is my string")

{"/"|str_replace:"-":$a}
// same as:
str_replace("/", "-", $a)

2 Comments

And what about passing no parameters?
@Hrvoje Golcic -- Just {my_function}: smarty.net/docs/en/language.function.function.tpl
0

The whole point of templating systems is to abstract the creation of views from the underlying language. In other words, your variables should be prepared for displaying before they are passed to a templating engine, and you should not use any PHP functions in the template itself.

6 Comments

So, is it impossible to do in Smarty? I'm re-purposing the data and don't think I should be passing it as a separate variable. In one scenario, I do need the HTML tags of $a; in another scenario, I don't.
I won't tell you if it's possible or not (it's been some time since I worked with Smarty). My point is, it is not something you should do.
Joshua Burns: usually for developers' confort. If not that, we'd still be doing all our coding in assembly.
@Mchl I think It's debatable. strtoupper for instance I think is nice to have in the view as the data is still the same, just presented in upper case.
Uppercasing a string and stripping html tags are quite different operations. While I can agree the former is well within the realm of data presentation, the latter imho is not.
|
0

Smarty already has a Language Modifier built in for this.

{$a|strip_tags}

You don't need Native functions as there already integrated into the plugin system

http://www.smarty.net/docsv2/en/language.modifier.strip.tags.tpl

others here:

http://www.smarty.net/docsv2/en/language.modifiers.tpl

2 Comments

@StackOverflowNewbie: Now I can tell you: it is possible ;)
Nothing is impossible !! (Apart from making the wife happy.. that's a tricky one) :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.