Ruby on rails 部分收益率的多重收益率
我有一个支持标题值和副标题值或块的分部:Ruby on rails 部分收益率的多重收益率,ruby-on-rails,layout,ruby-on-rails-4,partial,yield,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
<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>
我可能会使用多个屈服区域,而不是像那样使用局部变量。您可以通过以下方式完成此任务:
<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>