def tag_unless_empty(html_tag, options = {}, &block) html_options = options.stringify_keys if block_given? content = capture(&block) unless content.strip.empty? concat(tag(html_tag, html_options, true), block.binding) concat(content, block.binding) concat("#{html_tag}>", block.binding) end end end
Example:
<% tag_unless_empty :div, :class => "css_class" do %> <%# Nothing actually rendered here %> <%# or here %> <% end %>
produces nothing, whereas
<% tag_unless_empty :div, :class => "css_class" do %> <%= 'Something rendered here' %> <% end %>
produces
<div class="css_class">Something rendered herediv>