【问题标题】:Implications of calling yield() twice两次调用 yield() 的含义
【发布时间】:2011-02-16 13:29:16
【问题描述】:

两次调用yield() 的含义是什么?例如

- if yield :content_header
   yield :content_header
- else
  No Content Header

这会占用一点时间吗?我似乎无法获取 content_for?上班:/

【问题讨论】:

  • 这基本上不是你处理产量的方式。

标签: ruby-on-rails yield


【解决方案1】:

我相信这会将content_for 块中的所有内容运行两次,这没有多大意义。

有什么原因你不能处理content_for中的条件逻辑吗?:

<% content_for :content_header do %>
  <% if some_condition %>
    <%= render_some_important_thing %>
  <% else %>
    <%= render_absence_of_thing %>
   <% end %>
<% end %>

【讨论】:

    【解决方案2】:

    如果没有提供默认值,您可能想要内容的默认值?

    Ryan Bates 在他的代码 (www.railscasts.com) 中做了这样的事情:

    <title><%= h(yield(:title) || "Untitled") %></title>
    

    如果没有提供给定的标题或“无标题”。也试一试these

    【讨论】:

      【解决方案3】:

      两次调用 yield 会导致在当前范围内对块进行两次评估。

      【讨论】:

        猜你喜欢
        • 2016-07-17
        • 2023-01-17
        • 1970-01-01
        • 2022-12-09
        • 2012-11-05
        • 2015-03-02
        • 2014-08-24
        • 2018-08-12
        • 2016-10-17
        相关资源
        最近更新 更多