Reputation: 851
This is the situation:
application.html.erb
<html>
<title><%= yield(:title) + ' | ' if :title.present? %> Website Name</title>
....
home.html.erb
<% provide(:title, 'Home') %>
So I want to print a specific page title only if such a title is provided via provide
.
But it seems that all methods tried return the same thing either the variable :title
is provided or not
>> defined? :title
=> "expression"
>> defined? :anything
=> "expression"
>> :title.present?
=> true
>> :anything.present?
=> true
>> :title.nil?
=> false
>> :anything.nil?
=> false
Upvotes: 0
Views: 65
Reputation: 4561
I find the most convenient way is to add this to your application_helper.rb file:
def full_title(page_title)
full_title = "Your Site Title"
if page_title.empty?
full_title
else
"#{full_title} | #{page_title}"
end
end
Then on each page view you can put at the top something like:
<%= provide(:title, "My About Section") %>
And then in your application/layout view in the head section you can call it via:
<title><%=full_title(yield(:title))%></title>
This leaves the method "full_title" to check if anything was provided instead of logic in your application layout view.
Upvotes: 1