Scala `match` and `val` do not compile in a view template
I have the following code in Play 2.0 template:
@content.toString.lines.map{
case line => // i put `case` here as another attempt to make it work
line match {
case "" => @Html("")
case _ => <li>@Html(line)</li> /*CRASH*/
}
}
It fails on the marked line, saying that not found: value line
. The second variant of it:
@for(line <- content.toString.lines){
@line match { /*CRASH*/
case "" => @Html("")
case _ => <li>@Html(line)</li>
}
}
fails on the marked line, claiming that 'case' expected but identifier found
.
UPDATE:
Same thing goes for val
:
@val headID = "head"
comes up with illegal start of simple expression
.
UPDATE ENDS
I would like to know, what am I doing wrong and how to correctly implement the match-case
structure and val
assignment in Play's templates?
Using match
expressions in templates
You need to enclose in braces (“{” and “}”) the HTML content of your templates:
@for(line <- content.toString.lines) {
@line match {
case "" => { }
case _ => { <li>@Html(line)</li> }
}
}
In your specific case, the following code would read better IMHO:
@content.toString.lines.collect {
case line if !line.isEmpty => { <li>@Html(line)</li> }
}
Defining values
You can define values using the defining[T](value: T)(usage: T => Html)
helper:
@defining(1 + 2 * 3) { value =>
<div>@value</div>
}
我发现在外面添加一个{}以包含整个代码会起作用
@{content.toString.lines.map{ line =>
line match {
case "" => @Html("")
case _ => <li>@Html(line)</li>
}}
following seems to work for me
@content.toString.lines.map{ line =>
line match {
case "" => @Html("")
case _ => <li>@Html(line)</li>
}
hard on eyes, but you can look at target/scala-2.9.1/src_managed/main/views/html/index.template.scala
in the play project directory to see what its putting in string literals.
As for val assignment, I don't know, but @defining may help
链接地址: http://www.djcxy.com/p/62634.html上一篇: 模式匹配语言知识,模式匹配方法