Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active September 20, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggieb/136bca3bdfcde6380a169c28f43ac1c8 to your computer and use it in GitHub Desktop.
Save reggieb/136bca3bdfcde6380a169c28f43ac1c8 to your computer and use it in GitHub Desktop.
Add generic page titles
<!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>
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
@reggieb
Copy link
Author

reggieb commented Sep 20, 2018

A system to allow page title to be specified on each page, but with a fall back to an automated system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment