Add a default page title with HAML in Rails

In the PlanningAlerts project, we recently added a default page title for those pages we forget to add specific titles to. I added the initial code and Matthew did it properly cleaned it up, here’s the diff:

diff --git a/app/views/layouts/application.haml b/app/views/layouts/application.haml
index 27d81d9..6b42b26 100644
--- a/app/views/layouts/application.haml
+++ b/app/views/layouts/application.haml
@@ -2,10 +2,7 @@
 %html(xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml")
   %head
     %meta(content="text/html; charset=utf-8" http-equiv="Content-Type")
-    - if @page_title
-      %title PlanningAlerts | #{@page_title}
-    - else
-      %title PlanningAlerts | Email alerts of planning applications near you
+    %title PlanningAlerts | #{@page_title || "Email alerts of planning applications near you"}
     - if @rss
       %link(rel="alternate" type="application/rss+xml" title="RSS" href=@rss)
     = stylesheet_link_tag "memespring", "main", :media => "all"

I thought other people might find this little hack useful for adding default page titles to their HAML templated Ruby on Rails projects.

Comments

These are imported from this site's old blogging software and are kept for archival reasons.

I am no coder, but its a good seo move. Title is an important property.

Thanks Eri, that’s a good point that I hadn’t really considered.