W3docs

How to assign an array within a smarty template file?

In a Smarty template file, you can assign an array to a variable using the assign function.

In a Smarty template file, you can assign an array to a variable using the {assign} tag. The first parameter is the variable name, and the second parameter is the array.

Example:

Example of assigning an array to a variable in a Smarty template file

{assign var='my_array' value=[1, 2, 3]}

You can also use a Smarty variable as an array key, like this:

Example of using a Smarty variable as an array key in a Smarty template

{assign var='my_array' value=[$my_key => $my_value]}

You can also use the {foreach} loop to iterate over the array and print its values:

Example of using the {foreach} loop to iterate over the array and print its values in a Smarty template

{foreach from=$my_array item=val}
    {$val}
{/foreach}