Html 在 Play2 Scala 模板中声明变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12031146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:20:57  来源:igfitidea点击:

Declare variable in a Play2 scala template

htmlscalatemplatesplayframeworkplayframework-2.0

提问by travega

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

如何在 Play2 Scala 模板中声明和初始化要在本地使用的变量?

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"

采纳答案by virtualeyes

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

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

基本上,您必须包装要使用它的块

回答by ollie

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?

实际上,只要您之后不尝试更改变量的值,@c4k 的解决方案就可以工作(并且非常方便),不是吗?

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

<h1> Using title @title </h1>

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

在您所说的评论中,它被输入为 HTML 类型。但是,这仅在您尝试@title再次覆盖时才有意义,不是吗?

回答by Govind Singh

scala template supports this, you can define variable in template

scala 模板支持这个,你可以在模板中定义变量

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

if you want to change its value in 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>

回答by biesior

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:

virtualeyes 的解决方案是正确的解决方案,但还有其他可能性,您可以将视图的参数声明为通常具有默认值,在这种情况下,您将可以将其用于整个模板 + 您将保留更改它的可能性的controller

@(title: String = "Home page")

<h1>Welcome on @title</h1>

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:

请注意,Java 控制器无法识别模板的默认值,因此您每次都需要添加它们:

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

回答by c4k

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

如果您不想用@defining 包装所有内容,您可以这样做:

@yourVariable = { yourValue }

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

@defining 指令在模板中真的不可读......

回答by Suma

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:

有一个明显的解决方案看起来很干净,有时可能更受欢迎:在模板周围定义一个范围,在其中定义变量,然后让范围生成您需要的 html 代码,如下所示:

@{
  val title = "Home"

  <h1>Welcome on {title}</h1>
}

This has some drawbacks:

这有一些缺点:

  • you are generating your html as Scala NodeSeqthis 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):
  • 您以NodeSeq这种方式将 html 生成为 Scala ,有时可能会受到限制
  • 此解决方案存在性能问题:其中的代码@{似乎是在运行时编译的,因为为页面生成的 Scala 代码看起来像这样(删除了一些常用的 Twirl 内容):

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

...

回答by Spektakulatius

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

在旋转模板中,我建议使用定义块,因为

@random = @{
     new Random().nextInt
}

<div id="@random"></div>
<div id="@random"></div>

would result in different values when used multiple times!

多次使用会导致不同的值!

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

回答by Eassa Nassar

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