Declare a variable in a Play2 scala template

How do you declare and initialize a variable that will be used locally in the Play2 Scala template?

I have it:

@var title : String = "Home" 

declared at the top of the template, but it gives me this error:

 illegal start of simple expression """),_display_(Seq[Any](/*3.2*/var)),format.raw/*3.5*/(""" title : String = "Home" 
+53
html scala templates playframework
Aug 20 2018-12-12T00:
source share
8 answers
 @defining("foo") { title=> <div>@title</div> ... } 

basically, you have to wrap the block in which you are going to use it.

+48
Aug 20 '12 at 1:01
source share

Actually, the @ c4k solution works (and quite conveniently) until you try to change the value of the variable after that, right?

You simply place this at the top of the template:

 @yourVariable = {yourValue} 

or, if this is a more complex expression, you do this:

 @yourVariable = @{yourExpression} 

You can even work with things like lists:

 @(listFromController: List[MyObject]) @filteredList = @{listFromController.filter(_.color == "red")} @for(myObject <- filteredList){ ... } 

In this example, it will be

 @title = {Home} //this should be at beginning of the template, right after passing in parameters <h1> Using title @title </h1> 

In the comments you said, it is injected into the HTML type. However, this is only relevant if you try to overwrite @title again, right?

+36
Aug 04 '15 at 9:11
source share

Scala template supports this, you can define a variable in the template

 @import java.math.BigInteger; var i=1; var k=1 

if you want to change its value in the template

 @{k=2} 



example

 @(title:String)(implicit session:play.api.mvc.Session) @import java.math.BigInteger; var i=1; var k=1 ^ <div id='LContent_div@i'> ^ <div id='inner_div_@k'></div> ^ </div> 
+14
Jan 28 '14 at 5:37
source share

virtualeyes is the right solution, but there is another possibility: you can simply declare the view parameter, as usual, the default value, in which case you will get it for the whole template +, you will save the opportunity for changing it with the controller :

 @(title: String = "Home page") <h1>Welcome on @title</h1> 

controller:

 def index = Action{ Ok(views.html.index("Other title")) } 

Please note that the Java controller does not recognize default template values, so you need to add them every time:

 public static Result index(){ return ok(views.html.index.render("Some default value...")); } 
+11
Aug 20 2018-12-12T00:
source share

If you don't want to wrap all your content with @defining, you can do this:

 @yourVariable = { yourValue } 

The @defining directive is really unreadable in the template ...

+4
Mar 08 '14 at 19:32
source share

There is one obvious solution that looks pretty clean and might be preferable sometimes: define the area around the template, define your variable inside it and let the area create the html code you need, for example:

 @{ val title = "Home" <h1>Welcome on {title}</h1> } 

This has some disadvantages:

  • you generate your html as Scala NodeSeq in a way that can sometimes be limited
  • there is a performance issue in this solution: the code inside @{ seems to be compiled at runtime, because the Scala code generated for this page is like this (some normal Twirl things are removed):

Generated Code:

 ... Seq[Any](format.raw/*1.1*/("""<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Basic Twirl</title> </head> <body> """),_display_(/*9.10*/{ val title = "Home" <h1>Welcome on {title}</h1> }),format.raw/*15.10*/(""" """),format.raw/*17.5*/("""</body> </html>""")) } } } ... 
+2
Oct 17 '16 at 9:48
source share
 @isExcel= {@Boolean.valueOf(java.lang.System.getProperty(SettingsProperties.isExcel))} 
0
Oct 03 '16 at 11:45
source share

In twirl templates, I would recommend using a defining block, because

 @random = @{ new Random().nextInt } <div id="@random"></div> <div id="@random"></div> 

will result in different values ​​when reused!

 @defining(new Random().nextInt){ random => <div id="@random"></div> <div id="@random"></div> } 
0
Dec 13 '18 at 16:40
source share



All Articles