In his excellent introduction to Haml, Dan Grigsby introduces a play on Greenspun’s 10th Rule of Programming: Grigsby’s First Rule:
“Any sufficiently complicated rhtml partial contains an ad hoc, informally-specified, bug-ridden, implementation of half of Haml.”
So, what actually is Haml? According to Hampton Catlin, Haml’s inventor:
“Haml is a rejection of the status-quo in XHTML generation. Its not pure code, nor is it a text processing language. Its something different. A way to build XHTML the way that I feel is most natural to the problem domain.”
In addition, according to the Haml site, “Haml takes your gross, ugly templates and replaces them with veritable Haiku.” Let’s take a brief moment to explore what is meant by ugly templates, and what is actually considered to be veritable Haiku.
Here’s an example of an ‘ugly template’:
<div id="profile">
<div class="left column">
<div id="date"><%= print_date %></div>
</div>
<div class="right column">
<div id="email"><%= current_user.email %></div>
<div id="bio"><%= h current_user.bio %></div>
</div>
</div>
As you can see, it’s just a standard html/erb snippet. It probably doesn’t even look that ugly to you. However, and Grigsby explains this wonderfully in his article, there is not only a lot of repetition, but also a lot of unnecessary characters. Hampton believes that markup should be beautiful. And not any kind of beauty, he means the kind of beauty that makes you faster; the beauty of efficiency. So, in that spirit, the same snippet represented in Haml is as follows:
#profile
.left.column
#date= print_date
#address= current_user.address
.right.column
#email= current_user.email
#bio= h(current_user.bio)
Now, that might look a little strange, but you have to admit it looks a lot slimmer. Notice the use of significant white space to remove the html closing tags, the css style syntax to describe div ids and classes, and the removal for the need for awkward <%= %>
tags. If you’re to count the number of characters used, the haml version in this example is 64% the size of the html version. This means 36% fewer characters to type, and 36% fewer characters to read. Essentially 36% less noise. Antoine de Saint-Exupery may well have been considering Haml when he said that “Perfection is achieved, not when there is nothing left to add, but when there is nothing left to take away.”
There are mixed responses to Haml. A lot of Rubyists seem to compare it with Python, purely because of the use of significant white space. However, Hampton has never written a single line of Python, and just feels that whitespace can be a very good way to represent structure in documents. He sees Haml as being much more like YAML than Ruby due to its focus on document structure and data configuration.
There is always space for new ideas, and DHH agrees: “I’m not sure it’s my flavor, but I love the willingness to think different”. The community seems to agree too as there is a lot of activity and discussion regarding Haml. Hampton jokes about the growing community:
“Reading the Haml forums is a weird experience. Its like reading a love letter to a technology.”
Hampton feels that 20 minutes is all you’ll need to fall in love with the simplicity of Haml. However, Grigsby disagrees, he thinks that 2 minutes is all it takes.
For more information read Grigsby’s introduction, and check out the Haml website.