Reputation: 2118
In my RoR 4.2 application, all pages provide a :title, used in the HTML header <TITLE>
tag.
And dynamic pages also provide a :page_heading content, which is displayed in my application header layout.
To keep a consistent look, I want the title to be also displayed in the application header layout for static pages, i.e when :page_heading is not provided.
Here is the beginning code of a show view:
<% provide(:title, 'Managing business rules') %>
<% content_for :page_heading do %>
<h1>Business Rule: <%= @business_rule.name %></h1>
<% end %>
----
Here is the code embedded in the _header.html.erb application layout:
<h1> <%= yield(:page_heading.empty? ? :title : :page_heading) %> </h1>
So, for static pages :title is displayed in IceWeasel title bar, and in the application header as well, which is correct.
But, for dynamic pages :title is displayed in IceWeasel title bar, and also in the application header, where :page_heading is expected.
The condition on the symbol :page_heading.empty? does not work.
How can I specify this condition correctly and have the expected symbol provided to the yield function?
Thanks.
Upvotes: 1
Views: 1399
Reputation: 51717
You can check for the content by using content_for?
<h1>
<% if content_for? :page_heading %>
<%= yield :page_heading %>
<% else %>
<%= yield :title %>
<% end %>
</h1>
or in short:
<h1><%= yield(content_for?(:page_heading) ? :page_heading : :title) %></h1>
Upvotes: 8
Reputation: 4147
The answer by @Beerlington is right, I'll just add another option to DRY the code a bit.
<h1>
<%= yield(content_for?(:page_heading)? :page_heading : :title) %>
</h1>
Upvotes: 2