Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TYPO3 fluid array key increment by 1

Tags:

typo3

fluid

In fluid template I am displaying items list

<f:for each="{myItems}" as="myItem" key="key">
   {key}. myItem.name
</f:for>

Here I need to display serial number for each item. for example,

1. myitem one
2. myitem two
etc.

But in my case I used {key} for serial number. But its starting from 0.

0. myitem one
1. myitem two

So how to increment key by 1 for displaying only ?

Thanks.

like image 681
Ganybhat-Satvam Software Avatar asked Dec 26 '22 01:12

Ganybhat-Satvam Software


2 Answers

You can use iteration="" property and then cycle. It starts from 1 instead of 0.

<f:for each="{myItems}" as="myItem" iteration="itemIterator">
   {itemIterator.cycle}. myItem.name
</f:for>

tip: f:for iterator contains other useful properties, like isFirst, isOdd etc.

Check the wiki for more datails

like image 162
András Ottó Avatar answered Jan 02 '23 15:01

András Ottó


In Fluid standalone and TYPO3v8 and above:

<f:for each="{myItems}" as="myItem" key="key">
   {key + 1}. myItem.name
</f:for>

The key (no pun intended) is the MathExpressionNode and any implementation of Fluid which intentionally disables this implementation will not support this expression - in those cases, iterator.cycle is your friend.

like image 21
Claus Due Avatar answered Jan 02 '23 16:01

Claus Due