{foreach},{foreachelse}{foreach} is used to loop over a
single associative array,
unlike {section}
which is for looping over arrays of data.
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 NameTypeRequiredDefaultDescriptionfromarrayYesn/aThe array you are looping throughitemstringYesn/aThe name of the variable that is the current
elementkeystringNon/aThe name of the variable that is the current keynamestringNon/aThe 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.
NoteThe 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 item 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 item and key 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 item 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}
]]>
{foreach} with nested item and keyAssign 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}
]]>
.indexindex contains the current array index, starting with zero.
index example
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0}
Title
{/if}
{$i.label}
{/foreach}
]]>
.iterationiteration contains the current loop iteration and always
starts at one, unlike index.
It is incremented by one on each iteration.
iteration and index example.firstfirst is &true; if the current {foreach}
iteration is the initial one.
first property example
{foreach from=$items key=myId item=i name=foo}
{/foreach}
]]>
.lastlast is set to &true; if the current
{foreach} iteration is the final one.
last property example
{$prod}{if $smarty.foreach.products.last}{else},{/if}
{foreachelse}
... content ...
{/foreach}
]]>
.showshow 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.
.totaltotal contains the number of iterations that this
{foreach} will loop.
This can be used inside or after the {foreach}.
total property example
{if $smarty.foreach.foo.last}
{$smarty.foreach.foo.total} items
{/if}
{foreachelse}
... something else ...
{/foreach}
]]>
See also {section}
and $smarty.foreach.