Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neat way to conditionally test whether to add a class in HAML template [duplicate]

Possible Duplicate:
Append class if condition is true in Haml (with Rails)

I'm using a template that allows you to mark a list item as current (using class=current), highlighting it in a nav bar.

In HAML, this looks like:

%li.current
  Menu item A
%li
  Menu item B
%li
  Menu item C

I have this code in a Sinatra view and want to programmatically add the class=current, depending on a parameter to the view.

How do I do this in the neatest way possible?

Currently, I'm doing it like this:

  - if section == "pages"
    %li.current
      %a{:href => "#pages"} Pages
  - else
    %li
      %a{:href => "#pages"} Pages

Which feels too verbose.

like image 299
Daniel May Avatar asked Sep 07 '12 05:09

Daniel May


1 Answers

You can inline the conditional to determine if the class is needed for the %li

%li{ :class => ('current' if section == "pages") }
  %a{ :href => "#pages" } Pages
like image 109
mguymon Avatar answered Sep 22 '22 06:09

mguymon