0

我对 Scala 和 play 框架很陌生,并且在为表单中的复选框生成标签时遇到问题。标签是使用 play framework (2.6.10) 及其 twirl 模板引擎生成的。我也在使用play-bootstrap库。

下面是我的简化版form.scala.html

@(enrolForm: Form[EnrolData], repo: RegistrationRepository)(implicit request: MessagesRequestHeader)

@main("Enrol") {
    @b4.horizontal.formCSRF(action = routes.EnrolController.enrolPost(), "col-md-2", "col-md-10") { implicit vfc =>
        @b4.checkbox(enrolForm("car")("hasCar"), '_text -> "Checkbox @repo.priceCar")
    }
}

我无法“评估”这@repo.priceCar部分。它只是没有被评估,我得到了文字字符串“@repo.priceCar”。

根据有关字符串插值的播放框架文档,我应该使用$而不是@,但这也不起作用。

当我遗漏"字符串周围时,我会遇到各种错误。

我将不胜感激有关我必须做什么的提示。

4

2 回答 2

0

您的问题是编译器将字符串逐字读取为Checkbox @repo.priceCar.

您需要将字符串添加在一起或使用字符串插值来访问此变量,因为@在普通 Scala 字符串中不是有效的转义字符:

@b4.checkbox(enrolForm("car")("hasCar"), '_text -> s"Checkbox ${repo.priceCar}")

这是将变量repo.priceCar注入到字符串中,而不是仅仅repo.priceCar从字面上读取为字符串。

于 2018-08-06T12:09:33.830 回答
0

通常,当您想在使用的字符串中放置变量时$

var something = "hello" 
println(s"$something, world!") 

现在,如果有像user.username您这样的成员需要使用${user.username}

println(s" current user is ${user.username}")

所以总的来说,你需要@在 Playframework 的视图中使用转义字符,当你使用变量时,它将是:

s" Current user: ${@user.username}"

因此,该'_text值应如下所示:

'_text -> s"Checkbox ${repo.priceCar}" //we drop the @ because the line started with '@'
于 2018-08-06T16:07:15.283 回答