Play2 - Template -> incrementing

2019-04-10 02:33发布

问题:

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)
}

回答1:

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.



回答2:

to declare at template

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

for increment in template

@(i+=1)