I was looking at how content_for works and observed the block.call
in the capture_erb_with_buffer
method. It apparently magically writes to the buffer variable which is then pruned. However, this I believe is deprecated and you can just call <%=yield :tag%>
now. How does this work? If I call yield from an ERB template where does that yield to?
A simple code sample to illustrate the point would be greatly appreciated.
This little tiny method called execute
in ActionView::Base
explains it all.
http://google.com/codesearch/p?hl=en#m8Vht-lU3vE/vendor/rails/actionpack/lib/action_view/base.rb&q=capture_helper.rb&d=5&l=337
def execute(template)
send(template.method, template.locals) do |*names|
instance_variable_get "@content_for_#{names.first || 'layout'}"
end
end
The do |*names|... end
block is the one receiving the yield
. You'll notice that the @content_for_#{names.first}
matches up with the variable being set in the content_for
process.
It's called from AV::TemplateHandlers::Compilable in #render, and I would assume other places as well.
def render(template)
@view.send :execute, template
end