5

How do I declare and increment a variable in play2? (in .scala.html templates)

Pseudo code:

@counter
@for(l <- list){
<span class="data-@counter">


</span>
@(counter += 1)
}
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
Maik Klein
  • 15,548
  • 27
  • 101
  • 197

2 Answers2

18

Do you really need counter and incrementing? You can do this:

@for((l, index) <- list.zipWithIndex){
    <span class="data-@index">@l</span>     
}

Method zipWithIndex creates list of tuples.

Infinity
  • 3,431
  • 3
  • 25
  • 46
6

to declare at template

@import java.math.BigInteger; var i=1

for increment in template

@(i+=1)
Govind Singh
  • 15,282
  • 14
  • 72
  • 106