scalaplayframeworkscala-template

Scala template: tags with increasing id


I am new to the play framework and scala templates. I have to loop through a map, and create an input tag for each element, with ascenting tag id. So what I want to have in my resulting html is something like the following:

<li><input id="option1" type="checkbox"/><label for="option1">sometext</label></li>
<li><input id="option2" type="checkbox"/><label for="option2">sometext</label></li>
<li><input id="option3" type="checkbox"/><label for="option3">sometext</label></li>

The amount of inputs is dynamic, and their id needs to be ascending.

This is what I currently have, just looping through the map:

@for(c <- frage.getAllChoices){
    <li><input id="option1" type="checkbox"/><label for="option1">@c.getText()</label</li>
}

I tried using #define, but i can't change the value afterwards: it just prints the expression. And afaik creating a variable with "var" is not possible in play scala templates.


Solution

  • Actually in your case it should be an ID of the object not just incremented value, shouldn't it?

    Anyway if you just need incremented value, you can use zipWithIndex as showed here i.e.

    @for((c, index) <- frage.getAllChoices.zipWithIndex) {
        ...
    }