user2288625
user2288625

Reputation: 255

declaring and passing List in playframework scala templates

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

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

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}

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?

Thanks.

Upvotes: 4

Views: 1612

Answers (1)

Aerus
Aerus

Reputation: 4380

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"))

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

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>

  @* ... *@
}

Upvotes: 5

Related Questions