Is there a case statement in Haml-Coffee?
Asked Answered
H

3

8

I'd like to do the following in Haml-Coffee:

- case msg.type
- when "usertext"
    = msg.body          
- when "direct"
    = msg.content.kbcontent_body

But I get an error "Reserved word "case""

I suspect it's not supported by Haml-Coffee actually.

Hesychast answered 16/2, 2012 at 15:47 Comment(1)
Why is the rails tag on here if it's for coffeescript?Fontenot
P
1

There isn't a case statement in CoffeeScript. You want switch — the case keyword is the JavaScript equivalent of when, and like many dropped JavaScript keywords is reserved in CoffeeScript. Also, I'm not 100% positive and don't have Haml-Coffee to test right now, but I think you'll need to indent the body of the switch.

Pilate answered 16/2, 2012 at 17:23 Comment(0)
B
27

Before the question was edited, it's main phrase had been:

Is there a case statement in HAML?

The answer is: in vanilla Haml there indeed is case!

%p
  - case 2
  - when 1
    = "1!"
  - when 2
    = "2?"
  - when 3
    = "3."
Backer answered 14/12, 2013 at 8:59 Comment(4)
Well, this is true of mainline Haml, but it isn't true of Haml Coffee, which is what the question was about.Pilate
The question was: "Is there a case statement in HAML?". There is.Backer
The question specifically says "it's HAML-Coffee". You can go test it in Haml Coffee if you want to see for yourself — this code will not work. And BTW, revenge-downvoting is not classy and encourages silent downvotes, which isn't a good outcome.Pilate
Yeah but they put the ruby-on-rails tag on it totally confusing it.Fontenot
P
1

There isn't a case statement in CoffeeScript. You want switch — the case keyword is the JavaScript equivalent of when, and like many dropped JavaScript keywords is reserved in CoffeeScript. Also, I'm not 100% positive and don't have Haml-Coffee to test right now, but I think you'll need to indent the body of the switch.

Pilate answered 16/2, 2012 at 17:23 Comment(0)
A
0

You can use any complex ruby code in haml using :ruby syntax when it is extremely necessary. Keep this on top level and use the resulting variable in your coffee haml

:ruby
  x = case a
  when 2
     "two"
  when 3
     "three"
  end
Asseverate answered 3/2, 2023 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.