简体   繁体   English

如何使用Play Framework 2.0视图创建常量

[英]How to create a constant using play framework 2.0 views

Hi I'm using the play framework in Java 嗨,我在Java中使用play框架

I need to create a constant in a view and use it in a loop. 我需要在视图中创建一个常量,并在循环中使用它。

To illustrate this will be the code in a view 为了说明这一点,将使用视图中的代码

@for(i <- 1 to 7){
@if(i>=wd) {   //The constant wd is defined outside but in  
    <td>@cur++</td>
}else {
    <td></td>
    }
}   

(I have to use wd many times and I think its a little ugly to pass it from the controller). (我必须多次使用wd,我认为从控制器传递它有点难看)。 Is there no way to just create a constant? 有没有办法只创建一个常量?

I looked at 我看着

@defining(user.getFirstName() + " " + user.getLastName()) { fullName =>
  <div>Hello @fullName</div>
}

but this doesn't seem to help Thanks 但这似乎没有帮助,谢谢

The defining block should be exactly what you need for this: defining块应该正是您所需要的:

If your constant is limited to the template you simply wrap your whole template in the defining block: 如果您的常量仅限于模板,则只需将整个模板包装在定义块中:

@defining( 1 ){ wd =>

    @for(i <- 1 to 7){
        @if(i>=wd) {  
            <td>@cur++</td>
        } else {
            <td></td>
        }
    } 
}

You're also not limited to Integer s in there, you can define Strings, Lists, ... in there aswell. 您也不仅限于其中的Integer ,还可以在其中定义Strings,Lists...。

If you want to use that constant in multiple templates, consider putting it in an Enum and get the value from the enum in the defining block. 如果要在多个模板中使用该常量,请考虑将其放入Enum并从定义块中的枚举中获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM