Emmanuel Ballerini
Emmanuel Ballerini

Reputation: 684

Play with Scala: how to nest special characters

I am working with Play and Scala and can't seem to figure out this one.
In my controller, I have 2 maps with the same key but different values. Then, on the view, I iterate over the first map and try to use the key (coming from the iterator) to do a lookup on the second one. When I do this

@map1.map { f =>
    <span>Key: @f._1</span>
    <span>Value from second map: @map2.getOrElse(@f._1, "default value")
}

It doesn't compile. It complains with the following error message: "illegal start of simple expression" (pointing to the second @f._1).
It seems clear that the nested @ is what is causing the problem because if I replace the second @f._1 by a constant, it works fine.

@map1.map { f =>
    <span>Key: @f._1</span>
    <span>Value from second map: @map2.getOrElse("my known key", "default value")
}

Does anybody know if it is possible to nest these 2 @? I tried various things (double @, double quotes) but that didn't help?

Upvotes: 0

Views: 102

Answers (2)

Arseniy Zhizhelev
Arseniy Zhizhelev

Reputation: 2401

map and if in Play are not simple Scala. It's a special templating construction. Inside {} you may use any html-code including nested @-expressions.

On the contrary — @-expressions are mostly Scala (excluding maps and ifs, flatMaps, and other for-comprehensions).

Have a look at Play's templating engine (http://www.playframework.com/documentation/2.0.1/ScalaTemplates).

In your case @map2.getOrElse(f._1, "default value") is a simple Scala expression without ifs and maps. So @ is not needed here.

Upvotes: 1

grotrianster
grotrianster

Reputation: 2468

try it without using "@" like this:

@map1.map { f =>
<span>Key: @f._1</span>
<span>Value from second map: @map2.getOrElse(f._1, "default value")
}

Upvotes: 1

Related Questions