#30
May 11, 2007

Pretty Page Title

If you are like me, you avoid creating page titles because it is kind of a pain. But in this episode I will show you a clean way to add titles to your pages.
Download (15.1 MB, 5:16)
alternative download for iPod & Apple TV (7.6 MB, 5:16)
# application_helper.rb
def title(page_title)
  content_for(:title) { page_title }
end
<!-- layouts/application.rhtml -->
<title>Shoppery - <%= yield(:title) || "The Place to Buy Stuff" %></title>

RSS Feed for Episode Comments 35 comments

1. InMan May 11, 2007 at 03:07

Used @page_title all the time :)


2. peanut May 11, 2007 at 05:57

This screencast is useful for me. Can you create screencast about using HTML Tidy in Ruby on Rails? Thank you!


3. Jake May 11, 2007 at 10:21

Great screencast! I took your idea one step further and had my title helper return either the set title or a default if one isn't already set when called with no arguments. That way I can call the helper to set the title, or call it w/ no arguments when I want to use the title.


4. Ryan Bates May 11, 2007 at 10:38

Great idea Jake. I normally don't like giving methods a dual purpose, but in this case I think it works very well.


5. Carlos May 11, 2007 at 12:38

Hi! Could you make a cast for i18n and l10n? would be nice!

thanks! congratulations!


6. Rob May 11, 2007 at 13:59

I was wondering why..

content_for(:title) { page_title }

works fine but

content_for :title { page_title }

doesn't - it gives a 500 error. Sorry.. I'm new to Rails/Ruby.


7. David Parker May 11, 2007 at 14:27

Another great tutorial Ryan!


8. Zach Inglis May 11, 2007 at 15:29

Rob,

He says so during the screencast. It's due to the syntax reading past :title, I believe.

So use it with brackets for now.


9. Ryan Bates May 11, 2007 at 16:49

Yep, Zach's correct. Ruby gets confused when you don't wrap the :title parameter in parentheses so it spits out a syntax error. I'm guessing it's because it thinks the {} brackets are trying to represent a hash not a block.

Same goes for later on in the video where I use parenthesis when calling yield(:title) while adding the || symbol. This is because Ruby doesn't know where to put the parenthesis, it could be here:

yield(:title || "foo")

or here:

yield(:title) || "foo"

It defaults to the first one (IIRC) so we need to clarify.

Whenever there's any doubt, wrap the parameters in parenthesis.


10. Pimpmaster May 11, 2007 at 18:01

This is one of my favorite screencasts. I just redid all my views and they sure look purty!

Cheers!


11. weskycn May 12, 2007 at 01:15

great tutorial !!!!
thanks


12. maze May 12, 2007 at 01:58

another great screencast! After every casts I have to go through all my code and change/implement what I've just learned ;-)

I'd like to see more casts where you use helper methods, cause I think they are useful but I don't know when...

cheers


13. Pimpmaster May 12, 2007 at 04:05

I just realized one caveat here. This particulat helper only takes a double-quoted string, so something like:

<%= title @article.title %>

Does not work because you will explicitly have to set it to a string

<%= title @article.title.to_s %>

This is not the case if you use the classic instance variable solution.


14. Pimpmaster May 12, 2007 at 08:29

Woops! Ignore those = on the ERB tags.


15. Ryan Bates May 12, 2007 at 08:48

@Pimpmaster, really? I've used non double quoted strings before. What kind of variable is @article.title? If it's not a string, that may be why. You may want to do the "to_s" conversion inside the title helper method.


16. Rafael May 12, 2007 at 10:30

Hi
thanks a lot for your work. Thats really what I was looking for, made a !little! donation today, hope that I can continue to donate :-D

Hopefully you have a lot of fun by doing that and you continue making such great "snippets".

greetinx


17. Ryan Bates May 12, 2007 at 10:35

Thanks for donating Rafael! It really helps. I enjoy making the screencasts as well.


18. Robin Curry May 12, 2007 at 15:34

Hey Ryan, love the screencasts. Quick question... I noticed in this one that as you typed keyboard shortcuts they were displayed in the upper right corner of the screen. Just wondering how you did that. Is this a feature of Textmate?


19. Ryan Bates May 12, 2007 at 22:39

I'm displaying the key commands with a little utility called KeyCastr.

http://stephendeken.net/software/keycastr/


20. Rafael May 13, 2007 at 02:43

Hi Ryan,
I would have a suggestion for a screencast.
How do you debug? With ruby-debug or breakpointer?
What is the better choice? There are topics, that breakpointer is deprecated, isn't it?
May this would be a topic for a Railscast

greetinx


21. Cameron Singe May 13, 2007 at 05:17

Great cast!

Looks forward to another one


22. Ryan Bates May 13, 2007 at 08:40

I used to use breakpointer a lot, but the recent Ruby releases broke it, so I'm currently getting by with just echoing variables to the log/view. I really need to learn ruby-debug, and once I do you can expect there to be an episode on it. Thanks for the suggestion.


23. Paul Newer May 13, 2007 at 09:56

It's the 13th! Do you know what that means?!?! Time for a new screencast! Whereisit whereisit whereisit....

Great screencast as always Ryan, I actually find myself enjoying yours more then peepcode, so be sure that I'm going to convert the money I would have spent on their screencasts into donation money for yourself, great work!


24. lisa Jun 01, 2007 at 15:11

I do:

<title><%= h @page_title || "Set title in: views/#{params[:controller]}/#{params[:action]}.rhtml" %></title>


25. Foobar Jun 09, 2007 at 03:45

why dont you set the title in the controller so it can be used by several views?


26. martin Jun 09, 2007 at 04:10

I agree with foobar, seting title in view is kind of wrong, logic should go in controller


27. Ryan Bates Jun 09, 2007 at 11:42

I see the title as view logic. The reason is because if a different controller action renders the same view, it doesn't have to set the title again.

For example, on the "new.rhtml" page you don't want to have to set the title in both the "new" action and the "create" action in the controller (in case the validation fails and the "new.rhtml" view is rendered again).

Now, if the title is complex obviously you don't want to put all that logic in the view. In that case it should be moved into a helper method.

I suppose this is personal preference, but I find placing it in the view is better.


28. Luke Cowell Sep 15, 2007 at 08:48

Great site. This could probably have the refactoring tag added t it.


29. Randy Oct 23, 2007 at 08:32

I've went a bit further: I have two helpers, access_denied() and action_denied() that put appropriate output; they now both have the title() call in them to set the title correctly, which is really nice. :)


30. Tim Gossett Jan 05, 2008 at 08:31

I was setting @page_title in my controllers until I saw this screencast... thanks, Ryan!

I like the idea of using content_for, but I'm not sure I can perform some logic on it like I can with an instance variable. I'm using the title method in application_helper.rb, only setting an instance variable instead of content_for.

In my application.html.erb, I do:

<title>
  <% if @page_title.nil? -%>
    example.com | an example site
  <% else -%>
    <%= @page_title %> | example.com
  <% end -%>
</title>

Can the same effect be achieved with content_for?


31. Ryan Bates Mar 03, 2008 at 20:14

@Tim, yep, actually content_for is just setting an instance variable in the background. I believe it's called @content_for_title (or whatever you pass to the method). You can reference this instance variable directly if you need, or just stick with yield(:title) which basically does the same thing.


32. Jeff Dean Apr 17, 2008 at 15:33

I like this method a lot - I just made one small change:

content_for(:title) { h(page_title) }

Adding the "h" method right to the helper ensures that no matter where I display it, it's safe. I often yield that title in a few other places, like in an h2 element somewhere on the page itself.


33. kino May 04, 2008 at 03:32

Really great tutorials, as always.


34. Silumesii Jul 20, 2008 at 15:23

@Jeff Dean, Thanks for the tip about the "h" method. I was getting XHTML warnings about my titles before and that cleaned them right up.

@Ryan Bates, I think I have learned more about Ruby and Ruby on Rails from you than anyone on the planet. Thanks!


35. Ben Bodien Aug 15, 2008 at 04:30

Very useful tip, thanks!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player