I need this construction in my HAML code:
- if something1
%div.a
- elsif something2
%div.b
- elsif something3
%div.c
- else
%div.d
%div another content
I would expected I get something like:
<div class="a|b|c|d">
<div>another content</div>
</div>
But in the fact I get
<div class="a|b|c|d"></div>
<div>another content</div>
How I must to update my code, if I need to get:
another content?
I think you should create a helper method instead:
%div{:class => helper_method(useful_parameters)}
The really ugly way to accomplish this is with ternary operators (condition ? true_case : false_case)
which doesn't sound like a good solution given from the fact that you selected haml and want to have your code base clean.
@Ingenu's helper method looks like the smarter approach, but if you don't mind it quicker and dirtier, you could do:
- if something1
-divclass = 'a'
- elsif something2
-divclass = 'b'
- elsif something3
-divclass = 'c'
- else
-divclass = 'd'
%div{:class => divclass}
%div another content
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With