TYPO3 fluid array key increment by 1
Asked Answered
R

2

5

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.

Ransell answered 19/6, 2013 at 8:16 Comment(0)
R
6

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

Remus answered 19/6, 2013 at 8:20 Comment(1)
Thanks for the extension. The link is very usefull here.Indies
Z
5

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.

Zebapda answered 3/9, 2016 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.