Rails 部分布局具有名为 Yield 的 - 为什么从未使用 Yield 块?
我有一个带有布局的部分:
<%= render :partial => 'home/mobile/home', :layout => 'home/mobile/page', :locals => {:page => 'abc2'}%>
布局(page.html.erb)具有不同块的产量,例如:
<div data-role="header">
<%= yield :header %>
</div>
但是,从未使用此产量块,而主级布局文件确实按照预期产量。
是否无法将命名的 content_for/yield 块与部分布局一起使用?有解决方法吗?
我期望继承 - content_for :header 应该首先在部分布局中查找 yield :header ,如果失败,则在主布局文件中查找。但事实并非如此。部分布局的yield :header 被简单地忽略。
I have a partial, with a layout:
<%= render :partial => 'home/mobile/home', :layout => 'home/mobile/page', :locals => {:page => 'abc2'}%>
The layout (page.html.erb) has yields for different blocks, such as:
<div data-role="header">
<%= yield :header %>
</div>
However, this yield block is never used, while the main-level layout file does yield as one would expect.
Is it impossible to use named content_for/yield blocks with the layouts of partials? Are there workarounds?
I would expect inheritance-- content_for :header should first look for a yield :header in the partial's layout, and failing that, the main layout file. But this is not the case. The partial layout's yield :header is simply ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在与您类似的情况下,我用不带块的调用
content_for
替换了yield
。所以在你的例子中,这很简单:这对我有用。部分中的
yield
不会像您所建议的那样逐渐增加,这可能是一个功能或一个错误 - 但这似乎仍然是它在 3 年后的 Rails 4.1.8 中的工作方式:)In a situation similar to yours, I replaced the
yield
with a call tocontent_for
without a block. So in your example it would be simply:That worked for me. That
yield
s in partials don't trickle up as you suggest may be a feature or a bug - but that's still appear to be how it works in Rails 4.1.8, 3 years down the line :)解决方法是使用 blocks 将布局包装到辅助方法中(这应该能够正确产生)。
您可能想要修复有关原始问题的错误。
The workaround would be to wrap your layout into an helper method using blocks (which should be able to yield correctly).
You may want to fil a bug about the original problem.