An Evil Mad Scientist

they call me mad, but in fact I am actually quite happy.

Sunday, March 30, 2008

Mix XHTML with HAML

I am a huge fan of HAML for generating templates in Rails.

A feature I was previously unaware of in HAML is that you can mix regular XHTML code in with the HAML code. This is especially useful for inserting an XML prolog to your template layout. I recently saw this exemplified on Robot Has No Heart

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
%html{"xmlns"=>"http://www.w3.org/1999/xhtml", "lang"=>"en"}
  %head
    %title Layout Example
  %body= yield

Cool, huh?

As you may know, adding an XML prolog forces Internet Explorer 6 for Windows into Quirks Mode even though a doctype has been declared, while other browsers remain in Strict Mode. So this can be useful when you need to specify a massively different stylesheet to IE6 in Quirks Mode, while remaining in Strict Mode for your main stylesheet for all other browsers.

Another useful application of this technique is whipping together sites in StaticMatic. Textarea tags give me whitespace headaches in HAML, so now I just write the actual XHTML tag for it. No problems. I use StaticMatic to build page templates that will be ported over to another application. Yes… that’s how much I love HAML.

posted by Lorin Tackett at 3:51 pm  

Powered by WordPress