Scala 模板集变量 [英] Scala template set variable
问题描述
我是 Scala 的新手(Play 2 框架中的 Scala 模板),我想执行以下操作:传递参数 isEdit 并根据此参数定义一个值,伪代码:
I am new to Scala (Scala templates in Play 2 framework), I want to do the following: Pass a parameter isEdit and depending on this parameter, define a value, Pseudocode:
variable myTitle;
if(isEdit)
myTitle="edit question";
else
myTitle="create question";
如何在 Play 2.0 的 Scala 模板中制定这个?传递 isEdit 不是问题,只是创建新变量 myTitle.谢谢:-)
How to formulate this in a Scala Template in Play 2.0? Passing isEdit is not the problem, only creating the new variable myTitle. Thanks :-)
推荐答案
首先,查看 Playframework 文档,因为那里有很多关于模板的好信息.http://www.playframework.org/documentation/2.0.4/ScalaTemplates
First of all, look over the Playframework documentation as there's a lot of good info on templates there. http://www.playframework.org/documentation/2.0.4/ScalaTemplates
现在,如果您需要在整个模板中重复使用该值,那么您可以在模板的顶部(可能在任何@imports 之后)声明它:
Now, if you need to reuse the value throughout the template then you can declare it at the top of your template (probably after any @imports):
@myTitle = @{ if(isEdit) "edit question" else "create question" }
如果你只需要一个地方,那么你真的只需要 if-else 块:
If you only need it in one spot then you really just need the if-else block:
<h1>
@if(isEdit) {
edit question
} else {
create question
}
</h1>
这篇关于Scala 模板集变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!