1

Ok, I have this line in my file which is in HAML

- unless @place.blank?

I need to add a class to it, regardless of the condition.

I've tried doing this:

- {:class => ("extra_middle" unless place.blank?)}

That doesn't work. I've tried other variations to no avail.

EDIT

Please keep in mind that there is a dash which in HAML means do not render unless not blank, right?

EDIT 2

To explain a bit more, the class I'm trying to add is meant to hide that specific part unless someone clicks on a link which will then show it ...

So, the class is regardless of result of place being blank or not ...

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
user273072545345
  • 1,536
  • 2
  • 27
  • 57

2 Answers2

3

I usually use a ternary operation, e.g.:

%div{ class: (place.blank? ? nil : 'extra_middle') }

... or move into a helper, should it become any more complex than that.

SkyWriter
  • 1,454
  • 10
  • 17
  • that doesn't work. I got this error: ` syntax error, unexpected =>, expecting keyword_end :class => place.blank? ? nil : 'extra_middle'` ... I'm guessing it's not working due to the dash in the beginning of the line? I've tried it without the the line, and this is what I got: `Invalid filter name ":class => place.blank? ? nil : 'extra_middle'".` – user273072545345 May 22 '15 at 17:45
  • I've added a further clarification to the post regarding what I'm trying to do with the class. Hope it helps. – user273072545345 May 22 '15 at 17:53
  • Expanded the example, so it works as is when you paste it. Hope it helps. – SkyWriter May 22 '15 at 17:58
  • thanks. I just tried it. It doesn't work. This is the error I got: `unknown type of %string %div{ class: (place.blank? ? nil : 'extra_middle') } ` – user273072545345 May 22 '15 at 18:01
  • btw, if I'm reading your line correctly, it postulates that the css class will render if the place is blank, correct? – user273072545345 May 22 '15 at 18:01
  • that's not what I'm trying to do at all. I'm trying to have the css class render regardless of the output of the condition. – user273072545345 May 22 '15 at 18:02
0

Call me stupid here, but could you not just put the -unless place.blank? inside a div

 .extra_middle
    -unless place.blank?
      .Whatever else you need
Carpela
  • 2,155
  • 1
  • 24
  • 55