Using HAML for Rails or Sinatra For Readability and For Sanity
admin — Wed, 10/21/2009 - 01:33
Two popular templating formats in Rails are HAML and ERB. I did not use HAML initially because it was too DRY. That was my initial reaction to the new language. Or it was for people who put too much emphasis on "Don't Repeat Yourself" philosophy. But Jesse Crockett and his project forced me to learn HAML and somehow I don't regret that. I use HAML for all Ruby projects. But I also use ERB and I will explain why a bit later.
HAML is great and I was wrong. I realized I have an aversion towards unreadable code. I did not see myself reading and writing CFML tags for a long time that is why I gave up my job to be a bum (not really). Seriously I also hate Dreamweaver. Why do they create these products? There are always better open source alternatives.
Indenting and Outdenting
Tools matter though. For Ubuntu users, gEdit is quite powerful with the help of some extensions like gmate. Gmate comes with a lot of themes and snippets for Rails and Ruby and other languages. Most important thing is for you to know how to configure your editor to use 2 spaces for the tab if you're going to use HAML and SASS a lot. It's pretty easy to change on preferences.
The commands for indenting and outdenting in gEdit are CTRL+T and SHIFT+CTRL+T respectively. You probably know that.
The Peepcode screencast on HAML covers how to use tools for Mac users like textmate.
ID's and Classes
Writing in HAML feels like writing in CSS and not HTML. How?
#header.white
and
%div{:id=>"header", :class=>"white"}
yields the same output.
In normal practice we mix Ruby code with HAML code. Why? Because we probably need to do DOM Manipulation and jQuery stuff like in a recent project of mine wherein nearly every section of page has rotating items and a menu which has no clear relation to the slideshow has to be highlighted. You need to say that "Menu 1" is the cousin of "Div 1." Your code will look much like this:
-for hp in @home_page
%div{:id=>hp.category_id.to_s + "-hp", :class=>'news-front-div'}
Javascript hates HAML?
They are not best friends but they are more like relatives. HAML is sensitive and Javascript is indifferent. Both are not perfect but they have to live with each other. Greg Moreno explains how to share code between javascript and rails. I still have a few issues with using helpers but it's probably me doing things the old way. I still use ERB for code or sections that require javascript. And I just render that javascript file on a HAML template.
HAML in Rails
There are several extensions on scaffolding in Rails. Most popular I think si Ryan's nifty scaffolds. For people who use RSpec and really follow the BDD way of doing things, some of them may be using the RSpec HAML scaffold. But I have yet to try Norman's HAML scaffold.
HAML in Sinatra
My other blog uses Sinatra. Using HAML in Rails seems to be easier as it automatically recognizes whether you're using haml format through .haml extension. However in Sinatra, you have to specify it as I noticed nearly nothing is implied in Sinatra and that is basically its philosophy.
On your main application file, you should have something like this:
get '/foo' do
haml :foo #=> reads and renders view/foo.haml
end
Is it possible to mix HAML and ERB in Sinatra? That is something I will probably not even try. I have switched to using ERB for Sinatra for no serious reasons.