meteor-jade icon indicating copy to clipboard operation
meteor-jade copied to clipboard

Support for case statment

Open chompomonim opened this issue 10 years ago • 2 comments

It would be good if you would implement jade's case statment support

template(name='main')
  case friends
    when 0
      p you have no friends
    when 1
      p you have a friend
    default
      p you have #{friends} friends

chompomonim avatar Jul 08 '14 08:07 chompomonim

Actually Blaze doesn't support case statements. Since I don't want to add some runtime overhead, the jade compiler will have to internally transform the case block in if/else blocks. This is similar to what is already done to transform else if blocks here.

mquandalle avatar Jul 10 '14 23:07 mquandalle

I often use a logic helper to mimic a case or switch statement:

if is field 'ok'
  span OK
else if is field 'maybe'
  span Maybe
else
  span Nope

I've posted my logic helpers in this gist, but the is helper could be implemented simply like this:

Template.registerHelper("is", (first, second) => first === second);

The and helper is great for the following little trick* that works because Blaze doesn't output falsy values – so if isOk is false, nothing happens:

span.label(class=and(isOk 'label-success'))

And I use the tern (ternary) helper like this:

i.icon(class=tern(isFiltered 'filter' 'world')

* relying on the parenthesis syntax from dalgard:jade

dalgard avatar Oct 14 '15 14:10 dalgard