28

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?!?

Meltemi
  • 37,979
  • 50
  • 195
  • 293

3 Answers3

23

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
gunn
  • 8,999
  • 2
  • 24
  • 24
14

A little better:

 - flash.each do |key, value|
   .flash{:class => key}= value
bowsersenior
  • 12,524
  • 2
  • 46
  • 52
13

Adding a variation on the theme:

this erb

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

could be

=flash.each do |key, value|
  .alert{:class => "alert-#{key}"}
    =value

similar to How do I make dynamic ids in Haml?

Community
  • 1
  • 1
Asaf
  • 2,480
  • 4
  • 25
  • 33