9

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
  • Copy/paste mistake? The declaration says va, not val... Commented Jul 3, 2011 at 18:42
  • Possible duplicate of Declare variable in a Play2 scala template Commented Jan 8, 2016 at 17:46

2 Answers 2

7

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>
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.