<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Managing SEO-friendly HTML Titles with Rails</title>
    <link>http://www.robbyonrails.com/articles/2008/03/26/managing-seo-friendly-html-titles-with-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Managing SEO-friendly HTML Titles with Rails</title>
      <description>&lt;p&gt;I&amp;#8217;ve seen this come up a few times in the #rubyonrails &lt;span class="caps"&gt;IRC&lt;/span&gt; channel and figured that I&amp;#8217;d post a quick entry for future reference.&lt;/p&gt;


	&lt;h2&gt;Problem: &lt;span class="caps"&gt;HTML&lt;/span&gt; titles&lt;/h2&gt;


	&lt;p&gt;&lt;strong&gt;You want to have a clean way to manage the titles on your &lt;span class="caps"&gt;HTML&lt;/span&gt; pages.&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  &amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
      &amp;lt;title&amp;gt;Robby on Rails &amp;amp;mdash; Article Title Goes Here&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
      ...
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Possible Solution(s):&lt;/p&gt;


	&lt;p&gt;Since the &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag is usually declared in your layout, you need to be able to dynamically update this information from almost every action in your application.&lt;/p&gt;


	&lt;p&gt;Here are a few ways that I&amp;#8217;ve seen this handled.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Use a instance variable, which would have a default value and you could override it in any controller action&lt;/li&gt;
		&lt;li&gt;Use the &lt;code&gt;content_for&lt;/code&gt; method to manage it.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Let&amp;#8217;s take a few minutes to look at these two approaches.&lt;/p&gt;


	&lt;h3&gt;Instance Variable&lt;/h3&gt;


	&lt;p&gt;With the instance variable, you might end up with something like:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/views/layouts/application.html.erb
  &amp;lt;title&amp;gt;Robby on Rails &amp;amp;mdash; &amp;lt;%= @html_title || 'Default text here...' -%&amp;gt;&amp;lt;/title&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then in a controller action&amp;#8230;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/controllers/articles_controller.rb
  def show
    # ...
    @html_title = @article.title
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So, that&amp;#8217;s one way to handle it and is probably a more common way.&lt;/p&gt;


	&lt;h3&gt;The &lt;code&gt;content_for&lt;/code&gt; helper method approach&lt;/h3&gt;


	&lt;p&gt;This solution is very similar (and underneath uses an instance variable).&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ll use the &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M001069"&gt;content_for&lt;/a&gt; and a little &lt;code&gt;yield&lt;/code&gt; action.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/views/layouts/application.html.erb
  &amp;lt;title&amp;gt;Robby on Rails &amp;lt;%= (html_title = yield :html_title) ? html_title : '&amp;amp;mdash; Default text here...' %&amp;gt;&amp;lt;/title&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then we&amp;#8217;ll create a helper method.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/helpers/application_helper.rb
  def set_html_title(str="")
    unless str.blank?
      content_for :html_title do
       "&amp;amp;mdash; #{str} " 
      end
    end
  end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, instead of defining the &lt;span class="caps"&gt;HTML&lt;/span&gt; &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; value in the controllers, we&amp;#8217;ll just toss this into our html.erb files as necessary.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  &amp;lt;% set_html_title(@article.name) -%&amp;gt;
  ... rest of view
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;..and that&amp;#8217;s pretty much it.&lt;/p&gt;


	&lt;h3&gt;Which is the better solution?&lt;/h3&gt;


	&lt;p&gt;This is where we&amp;#8217;ll not find a lot of consensus amongst people. I&amp;#8217;m a fan of the &lt;code&gt;content_for&lt;/code&gt;-based approach and defining the title in views rather than in controller actions. I&amp;#8217;m an advocate of skinny controllers and while I&amp;#8217;m not a big fan of messy views, I believe that there is less overhead in managing this within the View-world.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d love to hear your thoughts on this. Perhaps you have a more eloquent for managing things like this? Do share. :-)&lt;/p&gt;
</description>
      <pubDate>Wed, 26 Mar 2008 16:41:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:de684f82-efe6-48b6-a6f5-68ea542d72ef</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2008/03/26/managing-seo-friendly-html-titles-with-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>html</category>
      <category>rails</category>
      <category>rubyonrails</category>
      <category>helpers</category>
    </item>
  </channel>
</rss>
