In Play! 1, it was possible to get the current index inside a loop, with the following code:
#{list items:myItems, as: 'item'}
<li>Item ${item_index} is ${item}</li>
#{/list}
Is there an equivalent in Play2, to do something like that?
@for(item <- myItems) {
<li>Item ??? is @item</li>
}
Same question for the _isLast and _isFirst.
ps: this question is quite similar, but the solution implied to modify the code to return a Tuple (item, index) instead of just a list of item.
Yes,
zipWithIndexis built-in feature fortunately there’s more elegant way for using it:The index is 0-based, so if you want to start from 1 instead of 0 just add 1 to currently displayed index:
PS: Answering to your other question – no, there’s no implicit
indexes,_isFirst,_isLastproperties, anyway you can write simple Scala conditions inside the loop, basing on the values of the zipped index (Int) andsizeof the list (Intas well).