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).
Balazs Halasy (22/02/2005 1:11 pm)
Balazs Halasy (06/11/2006 10:56 am)
Comments
Other parameters?
Friday 08 July 2005 1:04:20 am
Mindshare Tech
Re: Other parameters?
Wednesday 13 July 2005 3:06:32 pm
Alimi Nabil
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?
Wednesday 05 April 2006 1:20:26 pm
Nathalie Grimaud
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
Monday 16 January 2006 3:20:37 pm
AtR
[ {break} ] guess this breakes out of the foreach ??
[ {skip} ] and this jumps to next element ?
[ {continue} ] ??
working without modulo
Monday 30 January 2006 7:45:55 pm
James Ward
Delimiter ?
Friday 11 August 2006 12:51:19 pm
Alimi Nabil
Re: Delimiter ?
Monday 14 August 2006 1:54:51 pm
Lenara Anafieva
Tested using an example:
foreach<br>
{let $b=array( 1, 2, 3, 4, 5, 6, 7 )}
{foreach $b as $bitem}
{$bitem}
{delimiter}
<br>:DEL:<br>
{/delimiter}
{/foreach}
{/let}
I've got these on the output:
foreach
1
:DEL:
2
:DEL:
3
:DEL:
4
:DEL:
5
:DEL:
6
:DEL:
7
And output results are expected
Are you sure ??
Monday 06 November 2006 10:55:35 am
André
Warning: eZTemplate Oct 18 2006 18:16:03
Function "delimiter" is not registered
eZ Publish 3.8.5
Re: Are you sure ??
Wednesday 18 October 2006 4:14:47 pm
André
Get image width in Foreach loop
Wednesday 28 February 2007 10:54:34 pm
Paul Bolger
<div class="picleft" style="width:{$related.data_map.picture.content[portrait].width}px>
{attribute_view_gui attribute=$related.data_map.picture image_class=portrait}
</div>
Gives you an image wrapped in a div of the same width as the image.
'portrait' is the image alias.
beware of max=...
Friday 18 May 2007 10:53:37 am
Pike
{foreach $dummies as $dummy max=5}
does work on some installations, not in others. as correctly
documented here, it should be
{foreach $dummies as $dummy max 5}