yieldView中的yield magic如何工作?
我在查看 content_for是如何工作的,并在capture_erb_with_buffer
方法中观察了block.call
。 它显然是神奇地写入缓冲变量,然后被修剪。 但是,我认为这已被弃用,您现在可以直接调用 。 这是如何运作的? 如果我从ERB模板调用yield,那会产生什么?
一个简单的代码示例来说明这一点将不胜感激。
这个在ActionView::Base
称为execute
微小方法解释了这一切。 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
do |*names|... end
块是接收yield
块。 您会注意到@content_for_#{names.first}
与content_for
进程中设置的变量匹配。
它是从#render中的AV :: TemplateHandlers :: Compilable调用的,我也会假设其他地方。
def render(template) @view.send :execute, template end
http://google.com/codesearch/p?hl=en#m8Vht-lU3vE/vendor/rails/actionpack/lib/action_view/template_handlers/compilable.rb&q=execute&exact_package=git://github.com/payalgupta/todo- list.git&SA = N&CD = 17&CT = RC&升= 28
我不确定ERB级别的yield
函数如何,但我知道它在应用于布局时是如何工作的。
下面是一个示例layout.html.erb文件:
<%= @title || 'Plain Title' %> <%= yield :head %> <%= yield %>
我已经定义了4个yield(:head,:menu,:footer和default)和一个实例变量@title。
现在,控制器动作可以渲染插入这些点的视图。 请注意,视图在布局之前呈现,因此我可以在视图中定义像@title这样的变量,并在布局中定义它。
示例视图:“关于”页面
<% @title = 'About' %> <% content_for :menu do %> <%= link_to 'Back to Home', :action => :home %> <% end %> We rock! <% content_for :footer do %> An Illinois based company. <% end %>
编辑页面
<% @title = 'Edit' %> <% content_for :head do %> <% end %> <% form_for :thing, :html => {:class => 'edit_form'} do |f| %> ... <% end %>
您可以混合和匹配要放入数据的产量,以及content_for :something
出现的内容content_for :something
将在匹配的yield :something
插入yield :something
布局文件中的内容。
它甚至适用于partials,partial可以插入自己的content_for:某个块,它将被添加到任何其他content_for调用。
只是:
在方法中调用yield会执行通过块传递给方法的代码。
例如
def my_method yield end my_method { puts "Hello" } my_method { puts "World" }
这5行将产生以下输出到屏幕
Hello World
有关Ruby中的yield的详细讨论,请参阅以下页面: Ruby Yield