Static assignment of arrays in Smarty templates

Joor Loohuis, November 13, 2010, 19335 views.

Occasionally, you need to fill an array with values for use in a Smarty template without explicitly assigning it in your PHP code. Here are some tricks for creating arrays directly in a template.

Tags: , , , ,

Normally speaking, you will assign data to a Smarty template from your application, but there are situations where this is not practical or not necessary, for example because the data never change. In case of scalar data (numbers and strings), using literal values will suffice, but for structured data such as arrays it is useful to store these data in a Smarty variable. That allows you to use structures such as loops to generate output. The idea behind generating literal arrays is that you can use any PHP function as a modifier in a Smarty template. The value you apply the modifier to is used as the first parameter, and any additional parameters are provided after the modifier.

Let's start with simple numerical arrays. Of course you can use the {section} structure, but having a simple array at our disposal has its uses. We generate it with the range() function.

{assign var=nums value=1|range:5}

print_r($nums) would show

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

The range() function takes a minimum and a maximum value as parameters. If the first parameter is higher than the second, the array will be ordered from high to low. Since PHP 5.0 the range() function also accepts a third parameter with which the step size can be changed.

More interesting is the possibility to generate arrays with text in them. To this end we can also use a PHP function as a modifier.

{assign var=fruit value=','|explode:'apple,pear,banana,pineapple'}

print_r($fruit) would show

Array
(
    [0] => apple
    [1] => pear
    [2] => banana
    [3] => pineapple
)

Here we use the explode() function to split a literal string on commas, but of course you can use other separator characters if that is more convenient. You could even use preg_split() with a regular expression to implement more complex criteria for splitting strings. One interesting application of splitting a string into components is to split the script name of the page you're generating.

{assign var=path value='/'|explode:$smarty.server.SCRIPT_NAME}

In the case of this particular blog article, print_r($path) would show

Array
(
    [0] => 
    [1] => en
    [2] => article
    [3] => 125
    [3] => static_assignment_of_arrays_in_smarty_templates
)

As you can see, this little trick can be particularly useful for sites that use complex (semi)RESTful URLs, where you need to do something clever with the components. Taking literal text arrays one step further, we can combine two arrays into an associative array (struct, hash, whatever you want to call it):

{assign var=keys value='/,\s*/'|preg_split:'nl,              fr,     be,      de'}
{assign var=vals value='/,\s*/'|preg_split:'The Netherlands, France, Belgium, Germany'}
{assign var=countries value=$keys|@array_combine:$vals}

print_r($countries) would show

Array
(
    [nl] => The Netherlands
    [fr] => France
    [be] => Belgium
    [de] => Germany
)

Here we use preg_split() to break up the strings, because we want to get rid of the whitespace in the strings that we use to line up the elements, to make sure the two arrays we're combining have the same number of elements.

The examples above are just simple examples of creating data structures from text. You can take it a lot further, without significant performance penalties, since these data are part of the compiled templates that Smarty stores.

Social networking: Tweet this article on Twitter Pass on this article on LinkedIn Bookmark this article on Google Bookmark this article on Yahoo! Bookmark this article on Technorati Bookmark this article on Delicious Share this article on Facebook Digg this article on Digg Submit this article to Reddit Thumb this article up at StumbleUpon Submit this article to Furl

Talkback

respond to this article