【问题标题】:Multiple Yields From A Partial一个部分的多重收益
【发布时间】:2014-08-22 02:18:24
【问题描述】:

我有一个支持标题值和字幕值或块的部分:

<header class="PrimaryHeader">
  <h1 class="Title"><%= title %></h1>
  <% if defined? subtitle %>
    <div class="Subtitle"><%= subtitle %></div>
  <% else %>
    <%= yield %>
  <% end %>
</header>

我是这样渲染的:

<%= render layout: 'shared/headers/primary_header', locals: {title: "Edit Gallery"} do %>
  <div class="special">Subtitle</div>
<% end %>

如何还支持标题的可选块?

基本上相当于:

<header class="PrimaryHeader">
  <% if defined? title %>
    <h1 class="Title"><%= title %></h1>
  <% else %>
    <%= yield  %>
  <% end %>
  <% if defined? subtitle %>
    <div class="Subtitle"><%= subtitle %></div>
  <% else %>
    <%= yield %>
  <% end %>
</header>

【问题讨论】:

    标签: ruby-on-rails layout ruby-on-rails-4 partial yield


    【解决方案1】:

    我可能会使用多个产生区域,而不是像那样使用本地人。你可以这样做:

    <header class='PrimaryHeader'>
      <%= content_tag(:h1, yield :title, class: 'Title') if content_for?(:title) %>
      <%= content_tag(:h2, yield :subtitle, class: 'Subtitle') if content_for?(:subtitle) %>
      <%= yield %>
    </header>
    

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 2013-06-18
      • 2018-11-08
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2012-11-26
      • 2010-12-21
      • 2016-11-07
      相关资源
      最近更新 更多