Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created October 16, 2008 16:50
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 Oshuma/17196 to your computer and use it in GitHub Desktop.
Save Oshuma/17196 to your computer and use it in GitHub Desktop.
# app/helpers/application_helper.rb
# Sets the page title and outputs a container if passed in.
# ex. <%= title('Some Page', :h2) %> will return the following:
# <h2>Some Page</h2> as well as setting the page title.
def title(name, container = nil)
@page_title = name
content_tag(container, name) if container
end
<!-- in your layout file. ex. app/views/layouts/application.html.erb -->
<head>
<title><%= @page_title || controller.action_name.humanize %></title>
</head>
<!-- in some view file. ex. index.html.erb -->
<!-- note: <%= %> to output a tag -->
<%= title('Blawg Home', :h2) %>
<!-- which will set @page_title and output an h2 tag, like this: -->
<h2>Blawg Home</h2>
<!-- or if you don't want to return a content tag, call it like so: -->
<% title('Blawg Home') %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment