Last active
September 20, 2018 10:41
-
-
Save reggieb/136bca3bdfcde6380a169c28f43ac1c8 to your computer and use it in GitHub Desktop.
Add generic page titles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<title><%= page_title %></title> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
# Generates a page title for the current page | |
# To specify a particular page title in the page view add: | |
# | |
# <% content_for(:html_title) { 'Title' } %> | |
# | |
# If no `content_for(:html_title)` is set for the page, a default title will be used. | |
# The default is "Site Name | <controller_name> | <action_name> | <id if present>" | |
# The action name is left off for the index and show actions | |
# Just "Site Name" is shown on the root page | |
def page_title | |
if content_for?(:html_title) | |
content_for(:html_title) | |
else | |
elements = ['Site Name'] | |
elements << controller_name.titleize unless current_page?(root_path) | |
elements << action_name.titleize unless current_page?(root_path) || ['index', 'show'].include?(action_name) | |
elements << params[:id] if params[:id].present? | |
elements.join(' | ') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A system to allow page title to be specified on each page, but with a fall back to an automated system