foreach
Summary
Iterates over arrays in different ways.
Usage
{foreach <array> as [ $keyVar => ] $itemVar
[ sequence <array> as $sequenceVar ]
[ offset <offset> ]
[ max <max> ]
[ reverse ]}
[ {delimiter}...{/delimiter} ]
[ {break} ]
[ {continue} ]
[ {skip} ]
{/foreach}Description
This construct makes it possible to iterate over arrays in different ways. The loop can be tweaked using the parameters (see above).
Examples
Example 1
{foreach $objects as $object}
{$object.name} <br />
{/foreach}This example will print out the names of the objects that are stored in the $objects array. If this array stores 4 objects with the following names: "Emmett Brown", "Marty McFly", "Lorraine Baines" and "Biff Tannen", the following output will be produced:
Emmett Brown
Marty McFly
Lorraine Baines
Biff Tannen
Example 2
{foreach $objects as $index => $object}
{$index} : {$object.name} <br />
{/foreach}This example demonstrates how to create an iteration counter.
0: Emmett Brown
1: Marty McFly
2: Lorraine Baines
3: Biff Tannen
Example 3
{foreach $objects as $object sequence array( 'dark', 'light' ) as $style}
<div class="{$style}">{$object.name}</div>
{/foreach}This example demonstrates how to create a loop where the iterations are displayed using alternating styles (in this case dark, light, dark, light and so on).
Contributors
- Balazs Halasy Updated
- Mindshare Tech Commenter
- Alimi Nabil Commenter
- Nathalie Grimaud Commenter
- AtR Commenter
- Harry Oosterveen Commenter
- James Ward Commenter
Comments (6 historical)
Written by readers of the earlier documentation sites, kept as they were.
Other parameters?
Mindshare Tech ·
Any chance you might update this piece to list out what all the other parameters do? Thanks!
Re: Other parameters?
Alimi Nabil ·
Hi,
Take a look at this : http://ez.no/ez_publish/documenta...plate_functions/program_flow/section
The {foreach} statement is a replacement for the {section loop=..}.
Re: Other parameters?
Nathalie Grimaud ·
offset :
Determines the start of the loop array for the iterations, the value must be an integer.
max :
Determines the maximum number of iterations, the value must be an integer.
parameters
AtR ·
These parameters is not explained here or in the section part, so I start some guessing..
[ {break} ] guess this breakes out of the foreach ??
[ {skip} ] and this jumps to next element ?
[ {continue} ] ??
Re: parameters
Harry Oosterveen ·
Indeed {break} breaks out of the loop and continues after the it
Both {skip} and {continue} proceed with the next element, but if a {delimiter} is defined, it will be included after {continue}, but not after {skip}. See this example:
{let list = array( 1,2,3,4,5)}
<p>
{foreach $list as $var}
{delimiter}<br />{/delimiter}
{if $var|eq(2)}{skip}{/if}
{if $var|eq(4)}{continue}{/if}
{$var}
{/foreach}
</p>
{/let}
working without modulo
James Ward ·
If you miss the delimiter modulo combination provided by the old section structure you can accomplish the same thing with code like this:
{foreach $objects as $objectKey => $object} {if eq($objectKey|mod(2),0)} {node_view_gui view=line content_node=$object} {else} {node_view_gui view=line content_node=$object} {/if} {/foreach}