Getting Key from a For loop with a List (not a Map)
playframework-2.0, scala
Solution
You can zip the list with its indices:
@defining(List("foo", "bar", "baz")) { items =>
@for((item, i) <- items.zipWithIndex if i % 2 == 0) {
@item no @i <br/>
}
}
Which prints:
foo no 0
baz no 2
Problem
I have a List of Models (List items) that I loop over in my template : ``` @for(item <- items) { // ... } ``` I'd like to get the key/index position of the `item` in `items` for two reasons : - I will show something like `Item n° @key` - I'd like to show only 1/2 items (I suppose : `if (@key % 2)`) How can I get the key/index if using a List and not a Map ? Thanks for your help