Use HTML 4 in your application

When you set up an application, Rails outputs XHTML by default.

It is no secret that sending XHTML as text/html MIME type is bad. XHTML output has been opposed numerous times on the Rails core mailing list, but the complaints have been falling on deaf ears. Rails wants to be cool and that’s why they want to use technologies that seem most hip.

But, using XHTML in your app is as hip as sawing off the roof of your 1985 Yugo and pretending it’s a Cadillac.

That’s why Haml 2.0 supports HTML 4 output. Here is how you can enable it:

Haml::Template::options[:format] = :html4

module StandardistaHelper
  # override tag helper from Rails to disable self-closing tags
  # (there is no such thing in HTML)
  def tag(name, options = nil, open = false, escape = true)
    "<#{name}#{tag_options(options, escape) if options}>"

ActionView::Base.send :include, StandardistaHelper

Now, instead of sticking this in your environment.rb, you can install the Standardista plugin I just released. You only need Haml 2 included in your app (I prefer using the gem). View the source to see what Standardista does (it’s simple).

Test it with a Haml layout like this one:

!!! strict
    %title Standardista test
    %meta{ :content => "text/html; charset=utf-8", "http-equiv" => "Content-type" }
    = stylesheet_link_tag 'application'
    = yield

The result should:

In production, your HTML and CSS will also be smaller (have less whitespace) and therefore load quicker.