Our big celebrity charity news site runs on Rails, and I’ve just switched all the view code from erb to haml. I cut the total number of lines of view code from 2370 to 1788, which makes for a saving of 25%.
Note that I didn’t add any helper methods in the process, so the comparison should be quite fair. If I exclude blank lines from the comparison, the ratio is about the same, and it’s quite impressive given that a few of the views have significant text content inside them.
It took about half an hour to switch my brain over to thinking in Haml, and then it was plain sailing. To my eyes, the views are no less readable now, and are certainly more concise.
Deleting all those
<% end %> tags was enormously satisfying, and
almost made me miss Python syntax.
Here’s an example of a partial from the site before and after its conversion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
and in HAML:
1 2 3 4 5 6 7 8 9 10 11
Haml’s just seen its
2.0 release, it’s approximately
as fast as Erb, and the error messages are really pretty good
now. (Occasionally I left a trailing
%>, or forgot to indent after
- if ..., and these produced slightly obscure error messages.)