Question

I have a simple code in scala template for Play

@( val i = 0){
.......
....

i => i+1; // incremental 
}

But the above code is not working any idea how to fix this ?

Was it helpful?

Solution

You can fetch the index of the iteration in Scala for loop, just zipWithIndex your collection:

@for((day, index) <- model.days.zipWithIndex) {
    <li>Day @index is @day</li>
}

like described in other question

OTHER TIPS

Scala for loops are different to Java for loops. There is no loop index that gets incremented, rather successive values are taken from a sequence. So the loop you want is like this:

for(i <- 0 until 10) {
  ...
}

In a Play template, the above loop looks like this:

@for(i <- 0 until 10) {
   <p>number: @i</p>
}

The sequence in the above loop is 0 until 10, which is actually a range. If you want to use i to look up a value in an array, don't do that. Get the elements directly from the array instead:

@for(element <- myArray) {
  <p>@element</p>
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top