For ViewHelper <f:for>
Note
This reference is part of the documentation of Fluid Standalone. If you are working with Fluid in TYPO3 CMS, please refer to TYPO3's ViewHelper reference instead.
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>
Copied!
Output:
1234
Copied!
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>
Copied!
Output:
<ul>
<li>fruit1: apple</li>
<li>fruit2: pear</li>
<li>fruit3: banana</li>
<li>fruit4: cherry</li>
</ul>
Copied!
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>
Copied!
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>
Copied!
Go to the source code of this ViewHelper: ForViewHelper.php (GitHub).
Arguments
The following arguments are available for the for ViewHelper:
as
-
- Type
- string
- Required
- 1
The name of the iteration variable
each
-
- Type
- array
- Required
- 1
The array or \SplObjectStorage to iterated over
iteration
-
- Type
- string
The name of the variable to store iteration information (index, cycle, total, isFirst, isLast, isEven, isOdd)
key
-
- Type
- string
Variable to assign array key to
reverse
-
- Type
- boolean
- Default
- false
If true, iterates in reverse