开发者

Dynamic class name in HAML

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

<% flash.each do |key, value| %>
  <div class="flash &l开发者_高级运维t;%= 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?!?


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


A little better:

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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜