You must use CaptureHelper .
def my_div(some_options, &block)
content = capture(&block)
concat(content)
end
<% my_div([]) do %>
<p>The content</p>
<% end %>
def my_div(some_options, &block)
capture(&block)
end
<%= my_div([]) do %>
<p>The content</p>
<% end %>
Use capture + concat if you need to execute output. Use capture if you need to capture and then reuse content. If your block does not explicitly use <% =, then you MUST call concat (preferred method).
This is an example of a method that hides content if the user is not an administrator.
def if_admin(options = {}, &block)
if admin?
concat content_tag(:div, capture(&block), options)
end
end
<% if_admin(:style => "admin") do %>
<p>Super secret content.</p>
<% end %>
source
share