Templates scala模板中的匹配大小写不匹配';t工作,在游戏2中

Templates scala模板中的匹配大小写不匹配';t工作,在游戏2中,templates,playframework-2.0,Templates,Playframework 2.0,我在scala模板中的代码: @session.get("user.id") match { case Some(_) => "xx" case _ => "yy" } <a href="">Logout</a> 在生成的.template.scala中,它是: """ <body> """),_display_(Seq(/*11.4*/session/*11.11*/.get("user.id"))),format.raw/*1

我在scala模板中的代码:

@session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}
<a href="">Logout</a>
在生成的.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>
"""
更新1

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

@defining(session.get("user.id")) { x =>
    @x match {
        case Some(_) => { "xx" }
        case None => {"yy"}
    }
}
但它看起来很复杂

更新2

找到另一个简单的解决方案:

@{session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}}
@session.get("user.id").map { _ =>
    <a href="@routes.Users.logout">Logout</a>
}.getOrElse {
    Not logged
}
但在复杂的情况下,它不能很好地工作:

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

它可以工作,但不使用匹配大小写,我遇到了同样的问题。把箱子的右边用大括号括起来,为我解决了这个问题

这对我很有用:

@user match {
    case Some(user) => { Welcome, @user.username! }
    case None => { <a href="@routes.Application.login">Login</a> }
}
@用户匹配{
case Some(user)=>{Welcome,@user.username!}
案例无=>{}
}
在没有大括号的情况下,它在突出显示的匹配线上的{后面的空格中给出了一个错误。“'case'应为,但找到了标识符。”

如果我试图在开头的大括号前加上@,我也会犯这个错误,如下所示:

//This gives me the same error
@user match {
    case Some(user) => @{ "Welcome, " + user.username + "!" }
    case None => { <a href="@routes.Application.login">Login</a> }
}
//这给了我同样的错误
@用户匹配{
case Some(user)=>@{“欢迎,”+user.username+“!”}
案例无=>{}
}

我找到了解决以下问题的方法:

      <div class="col-md-9">
        @{
          articles collect { 
          case (title,time,shortcontent) => {
            Html(s"""
                <div class="blog-post">
                    <h2 class="blog-post-title"> $title </h2>
                    <p class="blog-post-meta"> $time </p>
                    <p> $shortcontent </p>
                    <hr/>
                </div>
             """)
            }
         }  
        }
    </div>  

@{
文章收集{
案例(标题、时间、简短内容)=>{
Html(s)
$title

$time

$shortcontent


""") } } }

其思想是返回一个字符串,然后使用Html方法将其转换。

如果将大小写的右侧部分括在括号中会怎么样?例如,
case Some({u)=>{“xx}
谢谢,但这不起作用注意:巧妙地使用空格和\n格式化代码也可能会触发与\@users match类似的错误{case Some(users)=>{\n@for(\n user{No users}}
//This gives me the same error
@user match {
    case Some(user) => @{ "Welcome, " + user.username + "!" }
    case None => { <a href="@routes.Application.login">Login</a> }
}
      <div class="col-md-9">
        @{
          articles collect { 
          case (title,time,shortcontent) => {
            Html(s"""
                <div class="blog-post">
                    <h2 class="blog-post-title"> $title </h2>
                    <p class="blog-post-meta"> $time </p>
                    <p> $shortcontent </p>
                    <hr/>
                </div>
             """)
            }
         }  
        }
    </div>