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.
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
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