两次致电yield()
会有什么影响?例如
- if yield :content_header
yield :content_header
- else
No Content Header
这会耗费一点时间吗?我似乎无法获得content_for?工作:/
答案 0 :(得分:2)
如果没有提供内容,您可能想要内容的默认值吗?
Ryan Bates在他的代码(www.railscasts.com)中做了类似的事情:
<title><%= h(yield(:title) || "Untitled") %></title>
产生给定标题或“无标题”如果没有提供。同时给出these一些镜头。
答案 1 :(得分:1)
调用yield两次导致在当前作用域中对块进行两次计算。
答案 2 :(得分:0)
我相信这会在content_for
块中运行两次,这没有多大意义。
是否有任何理由无法处理content_for
中的条件逻辑?:
<% content_for :content_header do %>
<% if some_condition %>
<%= render_some_important_thing %>
<% else %>
<%= render_absence_of_thing %>
<% end %>
<% end %>