Showing posts with label yield. Show all posts
Showing posts with label yield. Show all posts

Tuesday, May 19, 2009

Custom Titles in Rails

One thing I've learned while building rails sites is that changing things in the header on a view-by-view basis can be tricky. This starts becoming important when you start optimizing your page for Google. One thing Google likes is descriptive and non-repetitive title tags. To get view by view customized title tags try this.
  1. Add `<%= yield(:title) || "Default Title" %>` to your layout inside your title tag.
  2. Add `<% content_for(:title, "Custom Title") %> to your view file.
You can replace "Custom Title" with code such as `@object.name` or anything else that returns a string for even more dynamic titles. I've had to look this technique up more than once now so I'm blogging it in an effort to better remember it. I hope it is useful to you as well. You can use this technique to put other things in the header as well just use `yield` and `content_for` judiciously.