Documentation · ViewHelper reference

XSD schema file | vhs master

Iterator / ForViewHelper

Repeats rendering of children with a typical for loop: starting at index $from it will loop until the index has reached $to. /

Overview

The v:iterator.for view helper acts like a normal for loop. You can define a starting number, end number, and the stepping number added on each iteration.

Suggested use

Say you have 6 images of cute cats, named cat1.jpg to cat6.jpg, and you want to display them:

<img src="cat1.jpg" alt="Cat">
<img src="cat2.jpg" alt="Cat">
<img src="cat3.jpg" alt="Cat">
<img src="cat4.jpg" alt="Cat">
<img src="cat5.jpg" alt="Cat">
<img src="cat6.jpg" alt="Cat">

That sure is a lot of typing. With the v:iterator.for view helper you can just do:

<v:iterator.for from="1" to="6" iteration="i">
    <img src="cat{i.index}.jpg" alt="Cat">
</v:iterator.for>

Thats nice. Notice the iteration argument. This provides you with a local template variable (in this case i) that you can use to access current information about your iteration.

Another example

Say you want to display all odd numbers up to 1000. That would be a lot of typing, but with this view helper you can just do:

<v:iterator.for from="1" to="1000" step="2" iteration="i">
    {i.index}
</v:iterator.for>

This is where the step argument steps in (haha). It will increase (or decrease) your starting number until it reaches your end number by adding its value on each iteration.

Warning
Please be aware that a RuntimeException is thrown if your stepping number is illegal. That means you should not set it to 0 (obviously). If your end number is lower than your starting number, your stepping number must be negative.

  • Tag usage example

    <v:iterator.for iteration="NULL" to="123" from="123" step="123">
    	<!-- tag content - may be ignored! -->
    </v:iterator.for>
  • Inline usage example

    {v:iterator.for(iteration: 'NULL', to: 123, from: 123, step: 123)}

Arguments

Argument Description Type
from

Starting number for the index

The starting number of the for loop. This can also be higher than your end number, but then your stepping number must be negative.

integer
iteration

Variable name to insert result into, suppresses output

Provides a local template variable with the current iteration information. The following elements are available:

indexThe current index of the loop
cycleThe current iteration of the loop (starting at 1)
isFirst``TRUE`` if the current iteration is the first
isLast``TRUE`` if the current iteration is the last
isOdd``TRUE`` if the current iteration is odd
isEven``TRUE`` if the current iteration is even
string
step

Stepping number that the index is increased by after each loop

The stepping number is added on each iteration to your current index. This can be negative if your starting number is higher than your end number. It can never be 0.

integer
to

Number that the index needs to reach before stopping

The end number of the for loop. The loop continues until the end number is reached (inclusive). This can also be lower than your starting number, but then your stepping number must be negative.

integer
Required attribute

Revision history

commit 2901aa81ebe5fae84840673bba23f83d9ffa54ad
Author: Danilo Bürger <*****@hmspl.de>
Date:   Wed Oct 16 13:43:02 2013 +0200

    [FEATURE] Added v:iterator.for, improved v:iterator.loop

commit 0a0e3b1c04372bc96b195d2410cdb9df49dbfaad
Author: Danilo Bürger <*****@hmspl.de>
Date:   Sun Oct 27 20:06:06 2013 +0100

    [BUGFIX] Remove LF from iterators

commit fbd5a8704da209ae205ae323a7234c24974132f7
Author: Danilo Bürger <*****@hmspl.de>
Date:   Fri Nov 1 01:07:12 2013 +0100

    [BUGFIX] Fixed endless loop if step is misused

commit 3abbb9ab677027c2e312e9734e72f2aca096adc4
Author: Danilo Bürger <*****@hmspl.de>
Date:   Sat Nov 16 23:02:26 2013 +0100

    [BUGFIX] Fixed iteration array

commit f9faca970f0d537242651764bdf44734abafa187
Author: Björn Fromme <*****@bjo3rn.com>
Date:   Mon Dec 16 17:51:09 2013 +0100

    [TASK] Refactor class inheritance

commit da31bad1d9bb058c072ddb5591e8ed907a1ae8e1
Author: Claus Due <*****@namelesscoder.net>
Date:   Tue Dec 31 23:08:37 2013 +0100

    [TASK] Happy new year!

commit f6ffa08ff6d1d2bf2a14eed72fe46f9a5bdf2d38
Author: Danilo Bürger <*****@hmspl.de>
Date:   Fri Nov 1 03:44:00 2013 +0100

    [TASK] Documentation for v:iterator.for

commit da7677bc74f14e9a9300e4ba4487e52c3cb97642
Author: Claus Due <*****@namelesscoder.net>
Date:   Sun Apr 20 01:13:33 2014 +0200

    [TASK] Switch to namespaces

commit 8bb2fc5ee124ba19b0a0ab8e061c9a153fc4651f
Author: Xaver Maierhofer <*****@xwissen.info>
Date:   Sun Jan 18 17:56:13 2015 +0100

    [TASK] Reference Copyright to license.md file