Dynamic class name in HAML

Meltemi picture Meltemi · Dec 9, 2010 · Viewed 12.3k times · Source

Is there a better way to convert the following to HAML?

<% flash.each do |key, value| %>
  <div class="flash <%= key %>"><%= value %></div>
<% end %>

Best I can do is:

  - flash.each do |key, value|
    %div{:class => "flash " + key.to_s}= value

But it seems awkward. And .flash#{ key}= value doesn't seem to be right?!?

Answer

gunn picture gunn · Dec 16, 2010

If you're looking for something every-so-slightly terser, you can do this now in haml:

- flash.each do |key, value|
  .flash(class=key)= value