我有一个支持标题值和字幕值或块的分部:
<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>发布于 2014-07-01 15:17:14
我可能会使用多个屈服区域,而不是像这样使用本地。您可以这样做:
<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>https://stackoverflow.com/questions/24511666
复制相似问题