scala模板中的匹配大小写在play2中不起作用

Fre*_*ind 31 templates playframework-2.0

我在scala模板中的代码:

@session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}
<a href="">Logout</a>
Run Code Online (Sandbox Code Playgroud)

但是case ...直接显示在生成的html页面上:

match { case Some(_) => "xx" case _ => "yy" }  Logout
Run Code Online (Sandbox Code Playgroud)

在生成的.template.scala中,它是:

"""
<body>
"""),_display_(Seq(/*11.4*/session/*11.11*/.get("user.id"))),format.raw/*11.26*/(""" match """),format.raw("""{"""),format.raw/*11.34*/("""
    case Some(_) => "xx"
    case _ => "yy"
"""),format.raw("""}"""),format.raw/*14.4*/("""
<a href="">Logout</a>
"""
Run Code Online (Sandbox Code Playgroud)

但我在文档中看到它应该支持match case:https://github.com/playframework/Play20/wiki/ScalaTemplates

@connected match {

  case models.Admin(name) => {
    <span class="admin">Connected as admin (@name)</span>
  }

  case models.User(name) => {
    <span>Connected as @name</span>
  }

}
Run Code Online (Sandbox Code Playgroud)

UPDATE1

最后,我找到了一种工作方式:

@defining(session.get("user.id")) { x =>
    @x match {
        case Some(_) => { "xx" }
        case None => {"yy"}
    }
}
Run Code Online (Sandbox Code Playgroud)

但它看起来很复杂.

UPDATE2

寻找另一个简单的解

@{session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}}
Run Code Online (Sandbox Code Playgroud)

但它在复杂情况下效果不佳:

@{session.get("user.id") match {
    case Some(_) => {<a href="@routes.Users.logout">Logout</a>}
    case _ => "yy"
}}
Run Code Online (Sandbox Code Playgroud)

@routes.Users.logout不会被转换.

UPDATE3

这是一个getOrElse解决方案:

@session.get("user.id").map { _ =>
    <a href="@routes.Users.logout">Logout</a>
}.getOrElse {
    Not logged
}
Run Code Online (Sandbox Code Playgroud)

它有效,但它不使用 match case

Tim*_*Tim 56

我遇到了同样的问题.用花括号包围案件的右侧部分为我解决了问题.

这对我有用:

@user match {
    case Some(user) => { Welcome, @user.username! }
    case None => { <a href="@routes.Application.login">Login</a> }
}
Run Code Online (Sandbox Code Playgroud)

没有大括号,它在{匹配线突出显示后}的空格给出了错误."'案例'预计会发现标识符."

如果我尝试在开头大括号之前放置@,我也会给出错误:

//This gives me the same error
@user match {
    case Some(user) => @{ "Welcome, " + user.username + "!" }
    case None => { <a href="@routes.Application.login">Login</a> }
}
Run Code Online (Sandbox Code Playgroud)