Does Playframework 2.0.x support in templat

2019-04-17 18:43发布

I wonder if play 2.0.3 and higher supports else if in views? I only read that one have to code that way: if {...}else{if{...}else{...}} cannot believe that.

7条回答
手持菜刀,她持情操
2楼-- · 2019-04-17 19:24

The @Todd Flanders answer is right. In a wrapper @{}, you can write your normal Scala code. Example

@{
  if (profile.sex == 0) {
    <p class="col-md-6">Other</p>
  } else if (profile.sex == 1) {
    <p class="col-md-6">Male</p>
  } else {
    <p class="col-md-6">Female</p>
  }
}
查看更多
姐就是有狂的资本
3楼-- · 2019-04-17 19:24

I was also able to get

@{if (true) "foo" else if (true) "bar" else "baz"}

to work. Keep in mind that most programming languages do not support "else if" as a lexical token. They are separate commands. The block of code executed by the "else" command happens to be an "if" statement.

Note also that you can mix XHTML with the clause:

 @{if (true) <b>foo</b> else if (false) "bar" else "baz"}

I agree with biesior that it's usually a good idea to push state logic into the controller, then you can have different views for different states, with shared components having their own sub-views.

查看更多
做自己的国王
4楼-- · 2019-04-17 19:31

No, "else if" is not supported in scala templates: Does play framework 2.0 support nested if statement in template?

You can use pattern matching or you can put if inside else.

查看更多
看我几分像从前
5楼-- · 2019-04-17 19:32

Now if else if is supported in latest playframework. Discussion is here https://github.com/playframework/twirl/issues/33

You can use like this:

@if(something) {
  ...
} else if (somethingElse) {
  ...
} else {
  ...
}
查看更多
家丑人穷心不美
6楼-- · 2019-04-17 19:34

I used an @ before the second if :

@if (true) {
...
} else { @if (true) {
...
} else {
...
}}
查看更多
Melony?
7楼-- · 2019-04-17 19:35

You can use switch statement in Scala to achieve it.
Example:

if(x>2){
   <block 1>

} else if(x>0) {
   <block 2>

} else {
   <block 3>
}

Translated:

x match {
   case x if(x>2) => {<block 1>}
   case x if(x>0) => {<block 2>}
   case _ => {<block 3>}
}

Hope can help you in some cases and hope play framework will support else if soon.

查看更多
登录 后发表回答