"xx" case _ => "yy" } Logout -6ren">
gpt4 book ai didi

templates - 在play2中,scala模板中的区分大小写不起作用

转载 作者:行者123 更新时间:2023-12-03 08:54:24 28 4
gpt4 key购买 nike

我在scala模板中的代码:

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

但是 case ...可直接显示在生成的html页面上:
match { case Some(_) => "xx" case _ => "yy" }  Logout

在生成的.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>
"""

但是我在文档中看到,它应该支持 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>
}

}

UPDATE1

最后,我找到了一种工作方式:
@defining(session.get("user.id")) { x =>
@x match {
case Some(_) => { "xx" }
case None => {"yy"}
}
}

但这看起来太复杂了。

UPDATE2

查找另一个简单的解决方案:
@{session.get("user.id") match {
case Some(_) => "xx"
case _ => "yy"
}}

但是在复杂的情况下它不能很好地工作:
@{session.get("user.id") match {
case Some(_) => {<a href="@routes.Users.logout">Logout</a>}
case _ => "yy"
}}
@routes.Users.logout将不会转换。

UPDATE3

这是一个 getOrElse解决方案:
@session.get("user.id").map { _ =>
<a href="@routes.Users.logout">Logout</a>
}.getOrElse {
Not logged
}

它有效,但不使用 match case

最佳答案

我遇到了同样的问题。用大括号将案件的右侧部分括起来,为我解决了这个问题。

这对我有用:

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

如果没有大括号,则匹配行上的{后面的空格突出显示错误。 “预期为“案例”,但找到了标识符。”

如果尝试将@放在花括号前,我也会给我这个错误:
//This gives me the same error
@user match {
case Some(user) => @{ "Welcome, " + user.username + "!" }
case None => { <a href="@routes.Application.login">Login</a> }
}

关于templates - 在play2中,scala模板中的区分大小写不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9748258/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com