Declare variable in a Play2 scala template

匿名 (未验证) 提交于 2019-12-03 02:19:01

问题:

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

I have this:

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

回答1:

@defining("foo") { title=>   
@title
... }

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



回答2:

Actually, @c4k 's solution is working (and quite convenient) as long as you don't try to change the variable's value afterwards, isn't it?

You simply place this at the top of your template:

@yourVariable = {yourValue}

or, if it's a more complicated expression, you do this:

@yourVariable = @{yourExpression}

You can even work with things like lists like that:

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

For the given example, this would be

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

Using title @title

In the comments you said, that it gets typed to HTML type. However, that is only relevant if you try to overwrite @title again, isn't it?



回答3:

scala template supports this, you can define variable in template

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

if you want to change its in template value you can by

@{k=2}

example

@(title:String)(implicit session:play.api.mvc.Session) @import java.math.BigInteger; var i=1; var k=1 ^ 
^
^


回答4:

virtualeyes' solution is the proper one, but there is also other possibility, you can just declare a view's param as usually with default value, in such case you'll have it available for whole template + you'll keep possibility for changing it from the controller:

@(title: String = "Home page")  

Welcome on @title

controller:

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

Note that Java controller doesn't recognise templates' default values, so you need to add them each time:

public static Result index(){     return ok(views.html.index.render("Some default value...")); }


回答5:

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 a template...



回答6:

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:

  • you are generating your html as Scala NodeSeq this way, which may be limiting sometimes
  • there is a performance issue with this solution: the code inside of @{ 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*/("""                        <span class="str" bdsfid="539">Basic Twirl</span>                    """),_display_(/*9.10*/{             val title = "Home"                  

Welcome on {title}

}),format.raw/*15.10*/(""" """),format.raw/*17.5*/(""" """)) } } } ...


回答7:

@isExcel= {@Boolean.valueOf(java.lang.System.getProperty(SettingsProperties.isExcel))}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!