*

2007 / June 26th/ Custom page titles and more with Rails

Here’s a quick tip to get custom page titles and more with Rails. From the view. Like it should be. The key to how this all works out in the end is how Rails’ templates are rendered. In Rails, the action is rendered before the layout. This little nuance is all we need to get going.

First, let’s set up our ideal section. Let’s say we want a custom page title, and we want to highlight a certain section of our navigation. In our view (say app/views/account/index.rhtml), we’d like something like the following:

<%
@page_title = "Account Dashboard"
@section = :account
%>

<h1>Welcome to your account!</h1>

Now that we’ve used the @ symbol in front of our variable, it’s an instance variable and will be available throughout the rendering process. Now we can go ahead and edit our application layout (say app/views/layout/application.rhtml):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Awesome App | <%= @page_title || "Welcome to My awesome app!" %></title>
</head>

<body>
  <%= yield %>
</body>

</html>

The little snippet @page_title || "Welcome to My awesome app!" means print out whatever is in @page_title, or “Welcome to My awesome app!” if there’s nothing in @page_title.

Wala, just like that you have custom page titles (or meta keywords, javascripts, etc) in your app! Don’t forget, custom page titles are huge in search engine optimization. My search engine traffic increased tenfold when I implemented relevant page titles on Poetry with meaning last year.

20 Comments

comments feed

  1. Gravatar
    Dr J

    June 26th | #

    Any reason why you set this in the view and not in the controller? There is no real benefit to setting it in the view

  2. Gravatar
    Kyle

    June 26th | #

    Because it’s presentational data that belongs in the view. In my opinion, putting it in the controller is ugly ugly ugly when compared to how MVC should work. To me, the controller should just be a conduit between models and view. Many times, page titles don’t even have anything to do with Model data: they only have to do with what page you’re on.

  3. Gravatar
    Kyle

    June 26th | #

    Dr J–putting it in the view goes along with the whole idea of Model-View-Container a little better. Controllers should only be used to pass along data from the model to the view, and the view takes that data and displays it. Since the title is more of a display related item, it’s better to keep it in the view.

  4. Gravatar
    Kyle

    June 26th | #

    Uggh, the other Kyle beat me to the punch :-)

  5. Gravatar
    Kyle

    June 26th | #

    You know what they say about great minds… ;)

  6. Gravatar
    Dr J

    June 26th | #

    I understand the whole point and theory of MVC, I’ve just always seen this done in the controllers probably because of the process of setting in instance variable in the view code. If the view really is for displaying data than should the actually setting of that data take place in the view?? The view really should just display the data that was already processed and setup.

    Really its just a matter of taste in this case. I see your points

  7. Gravatar
    Kyle

    June 26th | #

    Well I guess here’s my analogy: Would you set your <h1> in the controller or the view?

    To me, the same goes for page titles. It may be in a different place in the source code, but it’s the same thing.

  8. Gravatar
    Dan

    June 27th | #

    Off topic kyle, what are you using for highlighting the source code?

  9. Gravatar
    Dr J

    June 27th | #

    Ok, I’m sold. I agree with you.

  10. Gravatar
    Kyle

    June 27th | #

    Dan: I’m using Dan Webb’s excellent CodeHighlighter

  11. Gravatar
    Dan

    June 28th | #

    Very nice, didn’t know this little js thingie… Thanks…

  12. Gravatar
    WJ

    June 28th | #

    That’s quite a tip there, Kyle. Setting variables and displaying them is pretty advanced. For your next tutorial could you please help me out with getting my characters onto the screen? I’ve heard of these keyboard things but I could really use some help from you. Thanks.

  13. Gravatar
    Kyle

    June 28th | #

    No problem WJ, I’ll get writing that right away.

  14. Gravatar
    Vince

    July 6th | #

    Railscast #30, Ryan Bates shows a nicer way to do this simple simple technique over a month before this post

  15. Gravatar
    Kyle

    July 6th | #

    Vince, thank you for being so polite, and linking to said Railscast. I apologize for sharing my knowledge. In the future, I’ll be sure to leave sarcastic comments on other’s blogs rather than helping the community.

  16. Gravatar
    Branstrom

    July 6th | #

    /me gives Kyle a hug, and proceeds to linked screencast

  17. Gravatar
    Greg

    July 8th | #

    Kyle — Long time reader but a comment lurker until now. Just wanted to say thanks for the tip and for putting Vince in his place…

  18. Gravatar
    Russo

    September 23rd | #

    oteQrf Hello
    I am Russo

  19. [...] you should change ‘your tag line’ and ‘yoururl.com’ to suit. I read a similar post on this, which used the slightly more [...]

  20. Gravatar
    Anthony

    February 17th | #

    You can also try the EasyTitles plugin : http://github.com/ahe/easytitles/tree/master

Make a Comment

don’t be afraid, it’s just text

Comments are parsed with Markdown. Basic HTML is also allowed.