kodal.de

it´s all about music, design and coding

Archive for the ‘ Smarty ’ Category

Smarty – Tips

February 10, 2010 Smarty, coding, php Comments

Find some tricks on how to use Smarty effectively here.

Array handling in Smarty

Arrays are a bit tricky to handle in Smarty. But only at first glance! If you know your way around, it´s pretty easy to cope with them. Note that you can use virtually every native php-function by using modifiers, which allow you to use smarty as a smart wrapper for all php-functions:

array_push

let´s say you want to push the value “new” to an existing array named $offers. You can simply do so by using this term:

{$offers|@array_push:"new"}

Note the @-identifier, which makes smarty process the given array as a whole, rather than iterating through each element of the array.

This approach has a disadvantage, though. After having executed array_push, the returning result will be piped to smarty and therefore we have an unwanted output of the function´s result.

We can simply avoid this by following this approach:

{assign var="void" value=$offers|@array_push:"new"}

By doing so, we are assigning the return value from array_push to a variable (named “void”). As a result, the return-value is being stored in a variable rather than leading to an unwanted output of the latter.

in_array

if you want to check whether a the given value “new” exists within a given $offers, try this:

{"new"|in_array:$offers}

Popularity: 24% [?]