Rails 如何在 erb 模板中生成多个块?
Rails 如何摆脱 .erb 文件中的以下内容?
<%= yield :sidebar %>
<%= yield :other_bar %>
<%= yield :footer %>
它们如何能够在相同的上下文中多次产生不同的符号?这是某种 Rails 魔法吗?
我完全熟悉:
def some_method(arg1, arg2, &block)
yield(:block)
end
据我所知,以下行不通:
def some_incorrect_method(arg1, &block1, &block2)
yield(:block1)
yield(:block2)
end
那么他们是如何做到的?他们如何让它发挥作用?
How does rails get away with the following in an .erb file?
<%= yield :sidebar %>
<%= yield :other_bar %>
<%= yield :footer %>
How 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
他们将一个符号传递给yield...
...而不是yield到不同的块。
它的工作原理更像是这样的:
They are passing a symbol into yield...
...not yielding to a different block.
It works more like this:
您的意思是 http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for ?
Do you mean http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for ?