{foreach},{foreachelse} {foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single 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. Note The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with name undefined does not throw an error, but leads to unpredictable results instead. {foreach} properties are index, iteration, first, last, show, total. The <parameter>item</parameter> attribute assign('myArray', $arr); ?> ]]> Template to output $myArray in an un-ordered list {foreach from=$myArray item=foo}
  • {$foo}
  • {/foreach} ]]>
    The above example will output:
  • 1000
  • 1001
  • 1002
  • ]]>
    Demonstrates the <parameter>item</parameter> and <parameter>key</parameter> attributes 'Tennis', 3 => 'Swimming', 8 => 'Coding'); $smarty->assign('myArray', $arr); ?> ]]> Template to output $myArray as key/val pair, like PHP's foreach. {foreach from=$myArray key=k item=v}
  • {$k}: {$v}
  • {/foreach} ]]>
    The above example will output:
  • 9: Tennis
  • 3: Swimming
  • 8: Coding
  • ]]>
    {foreach} with associative <parameter>item</parameter> attribute array('no' => 2456, 'label' => 'Salad'), 96 => array('no' => 4889, 'label' => 'Cream') ); $smarty->assign('items', $items_list); ?> ]]> Template to output $items with $myId in the url {foreach from=$items key=myId item=i}
  • {$i.no}: {$i.label}
  • {/foreach} ]]>
    The above example will output:
  • 2456: Salad
  • 4889: Cream
  • ]]>
    {foreach} with nested <parameter>item</parameter> and <parameter>key</parameter> Assign an array to Smarty, the key contains the key for each looped value. assign('contacts', array( array('phone' => '1', 'fax' => '2', 'cell' => '3'), array('phone' => '555-4444', 'fax' => '555-3333', 'cell' => '760-1234') )); ?> ]]> The template to output $contact. {foreach key=key item=item from=$contact} {$key}: {$item}
    {/foreach} {/foreach} ]]>
    The above example will output: phone: 1
    fax: 2
    cell: 3

    phone: 555-4444
    fax: 555-3333
    cell: 760-1234
    ]]>
    Database example with {foreachelse} A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty assign('results', $db->getAssoc($sql) ); ?> ]]> The template which display None found if no results with {foreachelse}. {$con.name} - {$con.nick}

    {foreachelse} No items were found in the search {/foreach} ]]> .index index contains the current array index, starting with zero. <parameter>index</parameter> example {foreach from=$items key=myId item=i name=foo} {if $smarty.foreach.foo.index % 5 == 0} Title {/if} {$i.label} {/foreach} ]]> .iteration iteration contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration. <parameter>iteration</parameter> and <parameter>index</parameter> example .first first is &true; if the current {foreach} iteration is the initial one. <parameter>first</parameter> property example {foreach from=$items key=myId item=i name=foo} {if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if} {$i.label} {/foreach} ]]> .last last is set to &true; if the current {foreach} iteration is the final one. <parameter>last</parameter> property example {$prod}{if $smarty.foreach.products.last}
    {else},{/if} {foreachelse} ... content ... {/foreach} ]]>
    .show show is used as a parameter to {foreach}. show is a boolean value. If &false;, the {foreach} will not be displayed. If there is a {foreachelse} present, that will be alternately displayed. .total total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}. <parameter>total</parameter> property example {if $smarty.foreach.foo.last}
    {$smarty.foreach.foo.total} items
    {/if} {foreachelse} ... something else ... {/foreach} ]]>
    See also {section} and $smarty.foreach.