部分的多重收益率

时间:2014-07-01 13:35:59

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

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

<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>

1 个答案:

答案 0 :(得分:0)

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

<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>