动态生成播放框架中复选框的标签

时间:2018-08-06 09:28:57

标签: scala playframework twirl play-bootstrap

我对Scala和play框架很陌生,在为表单中的复选框生成标签时遇到问题。标签是使用播放框架(2.6.10)及其旋转模板引擎生成的。我也在使用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”。

根据the play framework documentation regarding string interpolation,我应该使用$而不是@,但这也不起作用。

当我在字符串周围省略"时,会遇到各种错误。

我很想知道我必须做什么。

2 个答案:

答案 0 :(得分:0)

您的问题是编译器将字面量读为Checkbox @repo.priceCar

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

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

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

答案 1 :(得分: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 '@'
相关问题