{foreach},{foreachelse}
{foreach} is used to loop over an associative array as well a numerically-indexed array. Every {foreach} tag must be paired with a closing {/foreach} tag.
| Attribute Name | Type | Required | Default | Description |
| from | array | Yes | n/a | The array you are looping through |
| item | string | Yes | n/a | The name of the variable that is the current element |
| key | string | No | n/a | The name of the variable that is the current key |
| name | string | No | n/a | The name of the foreach loop for accessing foreach properties |
-
Required attributes are from and item.
-
The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores, like PHP variables.
-
{foreach} loops can be nested, and the nested {foreach} names must be unique from each other.
-
The from attribute, usually an array of values, determines the number of times {foreach} will loop.
-
{foreachelse} is executed when there are no values in the from variable.
-
{foreach} loops also have their own variables that handle properties. These are accessed with: {$smarty.foreach.name.property} with "name" being the name attribute.
-
{foreach} properties are index, iteration, first, last, show, total.
Sample Usage:
Home
{foreach item=crumbs from=$nav.breadcrumbs}
/ {$crumbs.title}
{/foreach}
Sample Output:
Home / Documentation / Web Designer / Smarty Template Engine / Tags / {foreach}
see also {$nav.breadcrumbs}