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 => "&mdash;" %>
<%= 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/.

May 03, 2007

Comments

toby June 17, 2007

hey patrick, i finally got around to installing this on a work project. killer!

Rik Hemsley July 01, 2007

Works perfectly, just what I needed, thanks!

Richard Crowley July 09, 2007

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

@title = options.gsub(/<\/?[^>]*>/, "")

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?

Jason L Perry July 09, 2007

Yes, that last commit just totally borked our application. Something like this might be more appropriate:

   def title(options, headline='')
-    # Remove markup from title
-    @title = options.gsub(/<\/?[^>]*>/, "")
-  
     # Save title
     if options.is_a? String
-       @title = options
+      @title = options.gsub(/<\/?[^>]*>/, "")
Jason L Perry July 09, 2007

Sorry, here: http://pastie.caboo.se/77309

Patrick Crowley July 16, 2007

My bad, guys. Should be fixed now.

Jonathan Dickinson July 25, 2007

Hectic! I love the simplicity!

Dave Bolton July 26, 2007

Thanks for introducing this at the Sydney Rails Group Patrick, works very nicely.

Jamie Hill August 22, 2007

I like this a lot. How about a variation for meta descriptions, keywords etc.?

Patrick Crowley August 22, 2007

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.

Nick Zadrozny August 27, 2007

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™)

Nick Zadrozny August 27, 2007

Oops. No comment formatting? :\

Patrick Crowley August 31, 2007

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.)

Jason Cave November 09, 2007

Great plugin! I have been looking for something like this for a while. Have been adding it to many of my sites. Nice job.

Scott Barr December 04, 2007

I just stumbled across this and it solves my page title problems perfectly. Thanks for the great plugin Patrick :)

Cameron Barrie April 29, 2008

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?

Gilbert June 22, 2008

Nice little plugin! Thanks for your work!

Dmytro Shteflyuk June 28, 2008

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

Boris Masis September 24, 2008

Perfect, exactly what I was looking for.

New comments are disabled right now. Once we finish migrating this blog, we'll restore them.