Headliner: DRY up your page titles
Headliner is a Rails plugin that makes it easier to assign and format page titles from your views.
Normally, if your Rails application has lots of actions and a shared layout, you might find yourself setting custom page title names in your controllers.
Here's an example:
class PagesController < ApplicationController
def about
@title = "About us"
end
end
Then, in your main layout, you might have something like this:
<head>
<title>My website<% if @title %>: <%= @title %><% end %></title>
</head
This works okay... but page titles don't really belong in controllers, do they?
So, by moving these titles into your views, we can DRY things up a bit and reinforce the MVC design pattern that's so fundamental to Ruby on Rails.
Install
To install, just add Headliner to your vendor/plugins directory:
script/plugin install git://github.com/mokolabs/headliner.git
Usage
First, add this code to your main layout:
<head>
<%= title :site => "My website" %>
</head>
Then, to set the page title, add this to each of your views:
<h1><%= title "My page title" %></h1>
When views are rendered, the page title will be included in the right spots:
<head>
<title>My website | My page title</title>
</head>
<body>
<h1>My page title</h1>
</body>
Options
Use these options to customize the title format:
- :prefix (text between site name and separator)
- :separator (text used to separate website name from page title)
- :suffix (text between separator and page title)
- :lowercase (when true, the page name will be lowercase)
- :reverse (when true, the page and site names will be reversed)
And here are a few examples to give you ideas.
<%= title :separator => "—" %>
<%= title :prefix => false, :separator => ":" %>
<%= title :lowercase => true %>
<%= title :reverse => true, :prefix => false %>
Dealing with special pages
How do you set the page title without showing it in the view?
<% title "My page title" %>
What if your view headline is different from your page title?
<%= title "My page title", "My headline" %>
Mr. T says, ‘Use my method, fool!’
Just like ERB's HTML safe method, you can invoke Headliner with a single letter alias.
<h1><%=t "My page title" %></h1>
Note: this alias conflicts with the translate method in the Rails Internationalization (I18n) API, since it provides the same alias. If you need I18n support, you should use one of these forks on Github.
How does it work?
Ruby on Rails renders actions before inserting them into layouts. So, if you set a variable in your view, it will be accessible in your layout. But, at first glance, it looks like you're using a variable (in the head) before it's been assigned a value (in the body). Cool, huh?
Contributors
Special thanks to James Chan, Nick Zadrozny, and Jordan Fowler.
Feedback
Comments and patches welcome at http://github.com/mokolabs/headliner/.

hey patrick, i finally got around to installing this on a work project. killer!
Works perfectly, just what I needed, thanks!
This is very cool, thank you. And also, nice last name, Other Mr. Crowley.
However, I found one problem: when I installed the plugin and began using it, Ruby complained that I was calling gsub() on a Hash. Tracing back, I found that commenting the line
in headliner.rb fixed the problem, but I'm confused as to why this was there anyway. Shouldn't that line be in the section that runs if options.is_a? String?
Yes, that last commit just totally borked our application. Something like this might be more appropriate:
Sorry, here: http://pastie.caboo.se/77309
My bad, guys. Should be fixed now.
Hectic! I love the simplicity!
Thanks for introducing this at the Sydney Rails Group Patrick, works very nicely.
I like this a lot. How about a variation for meta descriptions, keywords etc.?
That's probably beyond the scope of Headliner, Jamie.
Keywords aren't useful these days because of abuse by SEO folk, but description is still pretty useful, of course. However, many applications don't have useful descriptions for each page.
I'll consider doing a plugin in the future (if I find myself needing to add descriptions)... but I'm sure you can whip up a simple helper if you need this now.
Hey buddy. I remembered when we talked about this plugin at Korova back in the day and I didn't have an example of my usual approach to titles. The one I was experimenting with back then was overcomplex anyway. Anyway I finally got around to typing up my current technique at http://nick.zadrozny.com/post/9840594
So the downside to my approach is memorization and rote addition to every new project—which is more than enough to justify headliner, IMO. But since I've already paid that price the upside is it's very simple, trivial to customize and extend, non-magical, just as flexible, and potentially even more dry for multiple layout files. But to each his own.
You might like that textilize_without_paragraph bit in #t(). Makes for nice quote marks and apostrophes.
(Oh, by the way, I'm going to try to post the occasional Rails snippet at my tumblelog there but may end up creating a separate blog entirely for that stuff. Some Day™)
Oops. No comment formatting? :\
Yeah, I like your use of textilize_without_paragraph. Maybe I can roll that into Headliner down the road. Web typography can be tricky, though... people tend to have their own way of dealing with such things.
(Errr... you can do some HTML in comments, I believe.)
Great plugin! I have been looking for something like this for a while. Have been adding it to many of my sites. Nice job.
I just stumbled across this and it solves my page title problems perfectly. Thanks for the great plugin Patrick :)
I've added a description and keywords methods to headliner, so you can add both/either description or keywords meta tags in the same way.. I know they're not really used that much, but some clients are still demanding them.... Where should I send the source to?
Nice little plugin! Thanks for your work!
Do you have plans to put your plugin on Github? We are using submodules in our applications to manage plugins, and it would be pretty cool to have your plugin submoduled too.
Thanks, it's really useful
Perfect, exactly what I was looking for.
New comments are disabled right now. Once we finish migrating this blog, we'll restore them.