Skip to the content
Documentation

foreach

No ratings yet

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 parameters:

Parameter Description Value
sequence Defines a sequence which is iterated together with the normal loop parameter (see example below) array
offset Determines the start of the loop array for the iterations integer
max Determines the maximum number of iterations integer
reverse The loop is performed in the reverse order if this is set  

There are also a set of inline template loop control structures available, these need to be a direct child of the foreach structure:

  Description Value
delimiter Determines a block of template elements which should be placed in between two iterations mixed
skip Jumps to next iteration, does NOT output delimiter  
breaks Breaks out of the foreach  
continue Jumps to next iteration, but outputs delimiter  

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

Comments (11 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. Other parameters?

    Mindshare Tech ·

    Any chance you might update this piece to list out what all the other parameters do? Thanks!

      1. 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.

  2. 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} ] ??

    1. Re: parameters

      chs ·

      I was wondering the same about skip and continue.
      Extract from lib/eztemplate/classes/eztemplateloop.php:

      elseif ( $childFunctionName == 'continue' ){  $this->SkipSequenceIncrement = true;  break;}elseif ( $childFunctionName == 'skip' ){  $this->SkipSequenceIncrement = true;  $this->SkipDelimiter = true;  break;}

      So the difference seems to be that with "continue" {delimiter} will get displayed.

  3. 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}
  4. Delimiter ?

    Alimi Nabil ·

    is the {delimiter} statement deprecated ? It doesn't seem to work with foreach.

    1. Re: Delimiter ?

      Lenara Anafieva ·

      It works as expected.
      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

      1. Are you sure ??

        André ·

         {foreach $module_result.path as $path}  {if $path.url}    [LT]a href={cond( is_set( $path.url_alias ), $path.url_alias,                                        $path.url )|ezurl}[GT]{$path.text|wash}[LT]/a[GT]  {delimiter}  /  {/delimiter}  {else} {$path.text|wash}  {/if}  {/foreach}

        Warning: eZTemplate Oct 18 2006 18:16:03
        Function "delimiter" is not registered

        Exponential 3.8.5

        1. Re: Are you sure ??

          André ·

          update: It's because it's inside an if clause!!!

  5. Get image width in Foreach loop

    Paul Bolger ·

    Thanks Lukasz, for this answer on extracting an image width (useful for defining the width of a parent container in css).
    <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.

Comments

Add a comment

Comments are read by a moderator before they appear.