开发者

How does Rails yield to multiple block in the erb templates?

开发者 https://www.devze.com 2023-01-08 12:56 出处:网络
How does rails get away with the following in an .erb file? <%= yield :sidebar %> <%= yield :other_bar %>

How does rails get away with the following in an .erb file?

<%= yield :sidebar %>
<%= yield :other_bar %>
<%= yield :footer %>

H开发者_如何学JAVAow are they able to yield multiple times in the same context to different symbols? Is this some kind of rails magic?

I'm totally familiar with:

def some_method(arg1, arg2, &block)
 yield(:block)
end

To my knowledge following doesn't work:

def some_incorrect_method(arg1, &block1, &block2)
 yield(:block1)
 yield(:block2)
end

So how are they doing it? How do they make it work?


They are passing a symbol into yield...

yield :symbol

...not yielding to a different block.

It works more like this:

def some_method(arg1, arg2, &block)
  yield(:some_symbol1)
  yield(:some_symbol2)
end

some_method do |symbol|
  case symbol
  when :some_symbol1
     # do A
  when :some_symbol2
     # do B
  else
     # unrecognised symbol?
  end
end


Do you mean http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for ?

0

精彩评论

暂无评论...
验证码 换一张
取 消