NoobOnRails

Ruby on Rails tips, tricks and hints for the aspiring rails hero.



Tuesday, November 29, 2005

Dynamically setting the page title


Don't you hate manually editing the title tags for more than one page in your website? I used to. Ruby on Rails makes changing your page titles a bit easier to manage. In your layout, throw in

<%= @page_title || "your_default_app_title_here" %>

in betwteen your title tags. Now on any .rhtml page in which you don't want "your_default_app_title_here" to be the title, set the @page_title variable.

For example...

<% @page_title = "this page makes me happy with glee" %>

...Heck, say you got an incoming params you want to show up in the title, do so with a little...

<% @page_title = "tagged with #{params[:title]}" %>

...Voila! Magic. (for uber noobs, you gotta use the #{} to use ruby code in a string, if this wasn't in the <% %> tags and just in some text on the page itself, all you would need to do is (for this example) <%= params[:title] %> ...

1 Comments:

  • At 11:41 PM, Anonymous Anonymous said…

    Sweet... I am working on my own blogging engine to teach myself RoR and this is on of the seemingly infinte number of questions I had!

     

Post a Comment

<< Home