[2.0] Scala template "else if" syntax

6,403 views
Skip to first unread message

Tom Green

unread,
Feb 10, 2012, 2:58:37 PM2/10/12
to play-framework
Hi,

I'm trying out my first Scala templates and am curious if they are
supposed to support standard "else if" syntax? e.g., is the following
suppose to be possible?

@if(x == 1) {
one
} else if(x == 2) {
two
}

This syntax gives me compilation errors, and the only thing I can get
to work is overtly nesting the second "if" inside the else block:

@if(x == 1) {
one
} else {
@if(x == 2) {
two
}
}

It's not a huge deal to me, but I was just curious if this is the
known/intended behavior. I browsed through a bunch of the templates
in the samples but couldn't find any cases where they needed this
logic.

Thanks,

-Tom

Drew Kutcharian

unread,
Feb 10, 2012, 8:49:53 PM2/10/12
to play-fr...@googlegroups.com
You should use match/case instead.

See "Pattern Matching" at https://github.com/playframework/Play20/wiki/ScalaTemplates

-- Drew

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

Pascal Voitot Dev

unread,
Feb 11, 2012, 7:56:15 AM2/11/12
to play-fr...@googlegroups.com
The parser parses "else" but doesn't parse "else if"...
So yes, you should better use pattern matching

Pascal

Tom Green

unread,
Feb 11, 2012, 11:38:18 PM2/11/12
to play-framework
Thanks for the confirmation. Yes I know about pattern matching but my
actual use case was not a simple "x == 1" vs. "x == 2". My
conditional tests are not related enough to be unified by a single
pattern match. I've had to stick with the embedded @if inside the
else block.

-Tom

On Feb 11, 7:56 am, Pascal Voitot Dev <pascal.voitot....@gmail.com>
wrote:

Mike

unread,
Feb 9, 2013, 6:06:47 AM2/9/13
to play-fr...@googlegroups.com
match / case does not work either for anything which is not extremely simple, such as accessing a field. Bug #46. Marked as confirmed but it does not work in 2.0.4 (latest released version till few days ago) and I cannot upgrade now due to too many changes needed to go to 2.1.

I tried match / case first because it is what i needed.
I found that there is a bug. It does not work.
I tried "if" and spent 4 hours making a simple enum value test work.
Then I added the else if part and found that it is not working.
Then I read this saying that one should use match / case.

However "if else" is supposed to work in scala.

virtualeyes

unread,
Feb 9, 2013, 8:32:59 AM2/9/13
to play-fr...@googlegroups.com
heh, heh, yes, did the same, finding out that "else if" is not supported; would be nice to document this "feature" ;-)

Template layer, having zero IDE support, is true blind coding, and is by far the least fun aspect of Play development.

Scalate template engine suffers the same, so nothing specific to Play.

Lift has an advantage here in that you're always in Scala code (short of xml tag refs).

Mike

unread,
Feb 10, 2013, 7:03:12 PM2/10/13
to play-fr...@googlegroups.com
the whole thing is desperating. For ex. after putting all the embedded if blocks I am now trying to do some probably extremely weird thing. Get an array or list of string into a javastring array for jQuery...
Reply all
Reply to author
Forward
0 new messages