Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAML - if / elsif construction

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

?

like image 828
user984621 Avatar asked Mar 19 '12 16:03

user984621


2 Answers

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.

like image 86
Candide Avatar answered Nov 02 '22 00:11

Candide


@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
like image 10
GetSet Avatar answered Nov 02 '22 02:11

GetSet