【发布时间】:2011-02-16 13:29:16
【问题描述】:
两次调用yield() 的含义是什么?例如
- if yield :content_header
yield :content_header
- else
No Content Header
这会占用一点时间吗?我似乎无法获取 content_for?上班:/
【问题讨论】:
-
这基本上不是你处理产量的方式。
标签: ruby-on-rails yield
两次调用yield() 的含义是什么?例如
- if yield :content_header
yield :content_header
- else
No Content Header
这会占用一点时间吗?我似乎无法获取 content_for?上班:/
【问题讨论】:
标签: ruby-on-rails yield
我相信这会将content_for 块中的所有内容运行两次,这没有多大意义。
有什么原因你不能处理content_for中的条件逻辑吗?:
<% content_for :content_header do %>
<% if some_condition %>
<%= render_some_important_thing %>
<% else %>
<%= render_absence_of_thing %>
<% end %>
<% end %>
【讨论】:
如果没有提供默认值,您可能想要内容的默认值?
Ryan Bates 在他的代码 (www.railscasts.com) 中做了这样的事情:
<title><%= h(yield(:title) || "Untitled") %></title>
如果没有提供给定的标题或“无标题”。也试一试these。
【讨论】:
两次调用 yield 会导致在当前范围内对块进行两次评估。
【讨论】: