Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 498
rjc Avatar asked Jul 03 '11 18:07

rjc


1 Answers

Actually I have never seen @if nor have I tried PlayFramework. But if is what I think it is, it seems that when you actually try to ask for letter it's already out of scope. What happens if you re-arrange the brackets as follows?

@for(col <- List.range(0,12)) {
  <td>
    @if(col % 2 == 0) {
      @{val letter = someMap(col)
        <div class="z@(letter)@(letter)s"></div>
      }
    }
  </td>
}
like image 106
Daniel Hinojosa Avatar answered Sep 22 '22 19:09

Daniel Hinojosa