在playframework scala模板中声明和传递List [英] declaring and passing List in playframework scala templates

查看:87
本文介绍了在playframework scala模板中声明和传递List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个header.scala.html文件,该文件需要一个字符串和一个列表参数

I have a header.scala.html file that expects a string and a list parameter

@(title: String)(scripts: List[String])

其他scala文件将引用标头,并将传递特定列表,例如

The other scala files will reference the header and will pass specific lists eg

@import scala._
@{val jsList = List("a", "b")}
@views.html.header("title"){jsList}

但是,我遇到Compliation错误-类型不匹配;找到:play.api.templates.Html必需:java.util.List [String]

However I get Compliation error - type mismatch; found : play.api.templates.Html required: java.util.List[String]

肯定有一些语法问题,我看不到...有人吗?

There must be some syntax issue that I'm not seeing... Anyone?

谢谢.

推荐答案

您不能在Play模板中声明变量(类似变量). (这是有关此问题的google小组讨论)

You cannot declare variables (like that) in Play templates. (here's a google groups discussion about it)

您可以做的第一件事是,如果您只需要在模板中使用一次该值:

The first thing you can do is, if you only need the value once in your template:

@views.html.header("title")(List("a","b"))

请注意,您应该使用(),我相信{}之间的所有内容都被解释为HTML代码(因此,您的类型不匹配错误).

Note that you should use ( and ), I believe that everything between {} is interpreted as HTML code (hence your type mismatch error).

但是,如果您在模板中需要多次,则此方法不合适.然后,您可以使用defining块:

However, this isn't a suitable approach if you need it multiple times in your templates. You can then use defining block:

@defining(List("a","b")) { jsList =>

  @* using it once *@
  @views.html.header("title")(jsList)

  @* using it twice *@
  <p>My list contains @jsList.size elements.</p>

  @* ... *@
}

这篇关于在playframework scala模板中声明和传递List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆