Scala模板集变量 [英] Scala template set variable

查看:64
本文介绍了Scala模板集变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

现在,如果需要在整个模板中重用该值,则可以在模板顶部声明它(可能在任何@import之后):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆