Working With Arrays

An array is a variable that holds zero or more other values. The typical operations you will want to perform with are an array are to check how many elements are in the array, and to loop over the elements in the array.

Counting an Array

To determine the number of elements in an array, use the count modifier.

Important

When using modifiers with arrays, you will nearly always need to precede the modifier name with an @ symbol. This is to indicate the modifier should be performed on the array as a whole and not on each array element individually. So in this instance you would use @count as the modifier.

Referring to previous if/else block example, the $numberOfArticles variable probably won't actually exist (it was just used for the example). Realistically, if you want to output a list of articles you might have an array called $articles which holds the articles. The following code shows how would determine the number of articles.

Example A.7. Determining the size of an array.

{if $articles|@count == 0}

    No articles found.
    
{else}

    Output the articles here!

{/if}

Tip

You can assign values to temporary variables using the {assign} function. This function accepts two arguments: var and value. The var argument indicates the name of the variable to create (do not include the preceding dollar symbol), while the value argument accepts the value to assign. Thus, you could use {assign var=numberOfArticles value=$articles|@count} to create the variable $numberOfArticles.

Looping Over Arrays

The simplest way to loop over values in an array is to use {foreach}. This accepts two arguments: one called from (the array to loop over), and another called item (the name of the variable to store the element in). The item argument should not include the preceding dollar symbol.

Any template code you include between {foreach} and {/foreach} will be used for every element of the array (in order). The following listing demonstrates using {foreach}.

Example A.8. Looping over an array.

{foreach from=$articles item=article}
    {$article}
{/foreach}

Similar to if/else, you can also use {foreachelse}. Any code between this and the closing {/foreach} is executed if the array is empty.

Example A.9. Using {foreachelse}.

{foreach from=$articles item=article}
    {$article}
{foreachelse}
    There are no articles!
{/foreach}

You may instead prefer to use if/else combined with the {foreach}, rather than using {foreachelse}.

Example A.10. Using if/else with {foreach}.

{if $articles|@count == 0}
    There are no articles!
{else}
    {foreach from=$articles item=article}
        {$article}
    {foreachelse}
{/if}

Accessing an Array Element

As mentioned earlier in this appendix, there are both arrays you can loop over and associative arrays. While you can loop over associative arrays, typically you will just want to access a single element of the array directly. You can do so by using the array name, followed by a period, followed by the element name. For example, if you have an associative array called $article which has an element called title, you can access this value with $article.title.

Referring back to the {foreach} example, often you will loop over, say, an array of articles. Each element in this array will itself be an associative array.

Example A.11. Looping over an array then accessing another array's element.

{foreach from=$articles item=article}
    {$article.title}
{foreachelse}
    There are no articles!
{/foreach}