In Smarty, the foreach loop is used to iterate over an array or an object. The foreach loop can be combined with foreachelse to handle cases when there are no items to iterate over.

foreach, foreachelse

{{foreach $items as $item}}
    {{$item}}
{{/foreach}}
{{foreachelse}}
    No items found
{{/foreach}}

Arguments

The following arguments are available;

ArgumentDescription
fromSpecifies the array or object to iterate over
keyAssigns the current iteration key to a variable
itemAssigns the current iteration value to a variable
name

Assigns a name to the current loop

The name argument in the {foreach} loop can be used to access properties of the iteration, such as:

  • index: Returns the current iteration index (starts from 0).
    Example: {{$smarty.foreach.<name>.index}}
  • iteration: Returns the current iteration count (starts from 1).
    Example: {{$smarty.foreach.<name>.iteration}}
  • first: Checks if it is the first iteration (returns true or false).
    Example: {{$smarty.foreach.<name>.first}}
  • last: Checks if it is the last iteration (returns true or false).
    Example: {{$smarty.foreach.<name>.last}}
  • total: Returns the total number of iterations.
    Example: {{$smarty.foreach.<name>.total}}
  • show: Returns the number of iterations visible on the current page (used with pagination).
    Example: {{$smarty.foreach.<name>.show}}

Examples

See the example below in which a list of product-id's is used:

foreach

{{$producten = [6351,23122,392367]}} 

<ul>
{{foreach from=$producten name=products key=key item=data}}
   <li>{{$key}}: {{$data}}</li>
{{foreachelse}}
   <li>No products available</li>
{{/foreach}}
</ul>


Output

<ul>
   <li>0: 6351</li>
   <li>1: 23122</li>
   <li>2: 392367</li>
</ul>

foreach with sort and filter

In a foreach loop, you can use the modifiers sort and filter to sort the list/array of data or to filter specific information.

{{foreach from=$producten|sort:'product_id':'DESC'|filter:'category':'Jeans' name=products key=key item=data}}
   {{$data}}
{{/foreach}}

first

In some cases, it might be desirable to know when the foreach loop is iterating through the first row of the list. This can be determined using the 'first' parameter, which will have the value TRUE when it's the first row;
$smarty.foreach.<naam foreach>.first

foreach first

{{$productlist = [6351,23122,392367]}} 

{{foreach from=$productlist name=products key=key item=data}}
   {{if $smarty.foreach.products.first}}
     An overview of the available products:
   {{/if}}

   {{$data}}
{{foreachelse}}
   No products available
{{/foreach}}


Output

An overview of the available products:
6351
23122
392367

index

Returns the current iteration index, which starts from 0;
{{$smarty.foreach.<foreach name>.index}}

foreach index

{{$products = [6351,23122,392367]}} 

{{foreach from=$products name=products key=key item=data}}
   {{if $smarty.foreach.products.index eq 0}}
      An overview of the available products:
   {{/if}}

   {{$data}}
{{foreachelse}}
   No products available
{{/foreach}}


Output

An overview of the available products:
6351
23122
392367

last

In certain cases, it might be desirable to know when the foreach loop is iterating through the last row of the list. This can be determined using the 'last' parameter, which will have the value TRUE when it's the last row;
$smarty.foreach.<naam foreach>.last

foreach last

{{$producten = [6351,23122,392367]}} 

{{foreach from=$producten name=products key=key item=data}}
   {{$data}}{{if NOT $smarty.foreach.products.last}},{{/if}}
{{foreachelse}}
   No products available
{{/foreach}}


Output

6351,
23122,
392367