Marco Prins
Marco Prins

Reputation: 7419

Conditional class in HAML

I have this html

  <tr <% if ap.overdue? %>class = "overdue"<% end %> >
    <td><%= ap.id %></td>
    <td><%= link_to ap.affiliate.title, superadmin_affiliate_path(ap.affiliate) %></td>
  </tr>

How do I code the equivalent in HAML? In particular the first line, which assigns a class only if the if condition is true.

Upvotes: 17

Views: 7982

Answers (2)

Jared
Jared

Reputation: 2457

HAML has a nice built in way to handle this:

%tr{ class: [ap.overdue? && 'overdue'] }

The way that this works is that the conditional gets evaluated and if true, the string gets included in the classes, if not it won't be included.

Upvotes: 8

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Try this

%tr{ :class => ("overdue" if ap.overdue?) }

Upvotes: 33

Related Questions