TYPO3 Fluid use iterator for second array
arrays, content-management-system, iteration, loops, typo3
Solution
The `{bikeIterator}` is an object itself with several methods/properties you can use. I'll give you an example. Given you have 3 bikes the iterator gives you the following options:
1st Bike:
{bikeIterator.index} = 0
{bikeIterator.cycle} = 1
{bikeIterator.isFirst} = 1
{bikeIterator.isLast} = 0
{bikeIterator.isEven} = 0
{bikeIterator.isOdd} = 1
2nd Bike:
{bikeIterator.index} = 1
{bikeIterator.cycle} = 2
{bikeIterator.isFirst} = 0
{bikeIterator.isLast} = 0
{bikeIterator.isEven} = 1
{bikeIterator.isOdd} = 0
3rd Bike:
{bikeIterator.index} = 2
{bikeIterator.cycle} = 3
{bikeIterator.isFirst} = 0
{bikeIterator.isLast} = 1
{bikeIterator.isEven} = 0
{bikeIterator.isOdd} = 1
TYPO3 >= 8LTS
Since TYPO3 8LTS fluid is capable of dynamic varible assignent. So you can go with this solution:
<f:for each="{bikes}" as="bike" iteration="bikeIterator">
<div class="teaser-image"><f:image src="{bikesimages.{bikeIterator.index}}" alt="{bike.manufacturer} {bike.type}" />
</div>
TYPO3 <= 7LTS
Anyway in TYPO3 7LTS or lower you cant pass `{bikeIterator.index}` to `{bikesimages}` like you tried. I recommend to implement your own ViewHelper that takes the `{bikeIterator.index}` and the `{bikesimages}` objectStorage or array and returns the path for the image src.
So you can write afterwards in your template:
<f:image src="{myNamespace:myViewHelper(index:bikeIterator.index, bikesImgaes: bikesImages)}" alt="{bike.manufacturer} {bike.type}" />
In your ViewHelper you have something like this:
/**
* @param integer $index
* @param array $bikesImages
* @return string
*/
public function render($index, $bikesImages) {
return $bikesImages[$index] ?: 'path/to/dummyImage.png';
}
Problem
I am currently developing a TYPO3 extension where I work with two separate arrays: one contains the text information about products (bikes), the second contains the corresponding images (bikesimages). In the frontend template, I iterate through the products with a for loop. My question: how can I use the iterator of the for loop (bikeIterator) to access the same index of the second array? ``` <f:for each="{bikes}" as="bike" iteration="bikeIterator"> <div class="teaser-image"><f:image src="{bikesimages.{bikeIterator}}" alt="{bike.manufacturer} {bike.type}" /></div> ``` This does not work. How do I use the iterator correctly?