Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
For ViewHelper <f:for>
Loop ViewHelper which can be used to iterate over arrays.
Implements what a basic PHP foreach()
does.
Examples
Simple Loop
<f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
Output:
1234
Output array key
<ul>
<f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}"
as="fruit" key="label"
>
<li>{label}: {fruit}</li>
</f:for>
</ul>
Output:
<ul>
<li>fruit1: apple</li>
<li>fruit2: pear</li>
<li>fruit3: banana</li>
<li>fruit4: cherry</li>
</ul>
Iteration information
<ul>
<f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo" iteration="fooIterator">
<li>Index: {fooIterator.index} Cycle: {fooIterator.cycle} Total: {fooIterator.total}{f:if(condition: fooIterator.isEven, then: ' Even')}{f:if(condition: fooIterator.isOdd, then: ' Odd')}{f:if(condition: fooIterator.isFirst, then: ' First')}{f:if(condition: fooIterator.isLast, then: ' Last')}</li>
</f:for>
</ul>
Output:
<ul>
<li>Index: 0 Cycle: 1 Total: 4 Odd First</li>
<li>Index: 1 Cycle: 2 Total: 4 Even</li>
<li>Index: 2 Cycle: 3 Total: 4 Odd</li>
<li>Index: 3 Cycle: 4 Total: 4 Even Last</li>
</ul>
Source code
Go to the source code of this ViewHelper: ForViewHelper.php (GitHub).
Arguments
The following arguments are available for <f:
:
each
-
- Type
- mixed
- Required
true
The array or SplObjectStorage to iterated over
as
-
- Type
- string
- Required
true
The name of the iteration variable
key
-
- Type
- string
Variable to assign array key to
reverse
-
- Type
- boolean
If TRUE, iterates in reverse
iteration
-
- Type
- string
The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)