播放2.0创建变量并分配值 [英] play 2.0 creating a variable and assign values

查看:65
本文介绍了播放2.0创建变量并分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此问题进行了大量搜索.我只想在scala html模板中创建一个变量,并且需要一个临时变量.

I searched a lot for this issue. I just want to create a variable in a scala html template and needed a temporary variable.

我发现的唯一使有感觉"的解决方案如下:

The only solution I found and which makes "sense" is the following:

@defining("hello text") {testvariable =>
    <h1>output of variable: @testvariable</h1>
}

那真的是唯一的方法吗? Playframework的家伙认真吗?我不想将变量值从控制器传递到模板.我只想创建一个本地简单变量,然后在"if-block"中为其分配值,

Is that really the only way? Are the guys from playframework serious? I don't want to pass a variable value from controller to the template. I just want to create a local simple variable and assign a value to it in an "if-block", nothing else.

推荐答案

是的,我认为Play专家很认真.

Yes, I think that Play guys are serious.

将变量定义为模板的hardcoded元素有什么意义?好的,我知道您不想多次插入一个值,但是您使用@defining显示的方式可以解决此问题.另一方面-如果只想在if块中放置变量以用作条件,那也就没有意义,因为您还可以编写@if(1==1){ code }之类的东西来模拟某些行为.在其他情况下,变量应由控制器确定,为清楚起见,您可以使用一些Map作为示例,以将变量组从controllers传递到view.

What is the sense of defining variable as a hardcoded element of the template? Ok I understand that you doesn't want to insert the one value many times but the way you showed with @defining solves the problem. On the other hand - if you want to place variable only for using it as a condition in the if block, that doesn't make sense too, as you can just also write something like @if(1==1){ code } to simulate some behaviour. In other cases your variables should be determined by the controller, and to make it clear you can use for an example some Map for passing groups of variables from controllers to the view.

Play中的标量模板只是函数,这意味着您还可以调用其他函数或方法.例如,您可以调用某些getter或其他将执行并返回您想要的任何内容的方法.有很多示例,因此我将跳过这一部分.

Scala templates in Play are just functions, that means you can also call other function or method. You can for an example call some getter or another method that will do and will return anything you want. There are many samples so I'll skip this part.

如第二部分所述,如果您不喜欢 @defining方法,则可以在应用程序中随处创建一些超级简单的方法(让我们考虑使用app/controllers/Application.java)来进行存储并设置/获取一些价值.当然,如果您打算使用许多变量",最好将其存储在一个映射中,而不是为每个映射创建单独的getter和setter.

As stated in the second part if you don't like the @defining approach, you can just create some super-simple methods somwhere in your app (let's consider that's app/controllers/Application.java ) for storing and setting/getting some value. Of course if you plan to use many 'variables' it's better to store it in one map instead creating separate getters and setters for each one.

在您的Application控制器中,只需添加这些简单的方法(如果需要,还可以为其他类型创建自己的吸气剂)

in your Application controller, just add these simple methods (also create own getters for other types if required)

private static Map<String, Object> map = new HashMap<String, Object>();

// setter
public static void setValue(String key, Object value) {
    map.put(key, value);
}

// general getter would work well with String, also numeric types (only for displaying purposes! - not for calculations or comparisons!)
public static Object getValue(String key) {
    return map.get(key);
}

public static Boolean isTrue(String key) {
    return  Boolean.valueOf(map.get(key).toString());
}

public static Double getDouble(String key) {
    return Double.valueOf(map.get(key).toString());
}

因此,接下来您可以通过设置和读取Map键和值在view中使用它

So next you can use it in your view by setting and reading the Map keys and values

@Application.setValue("name", "Stefan")
@Application.setValue("age", 30)
@Application.setValue("developer", false)
@Application.setValue("task1", 123.5)
@Application.setValue("task2", 456.7)


<h1>ellou' @Application.getValue("name")!</h1>

<div>
    Today there are your  @Application.getValue("age") birthday!
</div>

<div>
    You are @if( Application.isTrue("developer")  ) {
        very big developer
    } else {
        just common user
    }
</div>

<div>
    Once again: @{ val status = if (Application.isTrue("developer"))  "veeeeery big developer" else "really common user"; status }
</div>

<div>
    Today you earned $ @{ Application.getDouble("task1") + Application.getDouble("task2") }
</div>


<div> etc... </div>

如您所见,您甚至可以执行一些基本操作,无论如何,对于更复杂的任务,我都会将权重从(仅)模板引擎重定向到专用控制器的方法.

As can you see, you can even perform some basic operations, anyway for more sophisticated tasks I would redirect the weight from the (just) template engine to dedicated controller's methods.

这篇关于播放2.0创建变量并分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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