How do you declare and initialize a variable to be used locally in a Play2 Scala template?
I have this:
@var title : String = \"Home\"
There is one obvious solution which looks quite clean and may be preferred sometimes: define a scope around the template, define your variable inside of it, and let the scope produce the html code you need, like this:
@{
val title = "Home"
Welcome on {title}
}
This has some drawbacks:
NodeSeq this way, which may be limiting sometimes@{ seems to be compiled runtime, because the Scala code generated for the page loooks like this (some usual Twirl stuff deleted):The generated code:
...
Seq[Any](format.raw/*1.1*/("""
Basic Twirl
"""),_display_(/*9.10*/{
val title = "Home"
Welcome on {title}
}),format.raw/*15.10*/("""
"""),format.raw/*17.5*/("""
"""))
}
}
}
...