【发布时间】:2011-08-14 04:09:45
【问题描述】:
在 ERB 中编写此代码最易读和/或最简洁的方式是什么?编写我自己的方法并不可取,因为我想将一个更清洁的解决方案传播给我公司的其他人。
<% @items.each do |item| %>
<% if item.isolated? %>
<div class="isolated">
<% end %>
<%= item.name.pluralize %> <%# you can't win with indentation %>
<% if item.isolated? %>
</div>
<% end %>
<% end %>
== 更新 ==
我使用了一个更通用的 Gal 答案版本,它与标签无关。
def conditional_wrapper(condition=true, options={}, &block)
options[:tag] ||= :div
if condition == true
concat content_tag(options[:tag], capture(&block), options.delete_if{|k,v| k == :tag})
else
concat capture(&block)
end
end
== 用法
<% @items.each do |item| %>
<% conditional_wrapper(item.isolated?, :class => "isolated") do %>
<%= item.name.pluralize %>
<% end %>
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby erb