How to declare and access local variables in scala template in play framework?

Viewed 4843

I have code in scala template like:

@for(col <- List.range(0,12)) {
    <td>
        @if(col % 2 == 0) {
            @{ val letter = someMap(col) }
            <div class="z@(letter)@(letter)s"></div>
        }
    </td>
}

But I get compile error: value letter not found. How can I declare variables and be able to access later in the markup like above?

2 Answers

The only way I got this working on Play Framework 2.8.x was with Scala's Range function:

@import scala.collection.immutable.Range

@for(col <- Range(0,12)) {
  <td>
    @if(col % 2 == 0) {
      @{val letter = someMap(col)
        <div class="z@(letter)@(letter)s"></div>
      }
    }
  </td>
}
Related