I wanted find out for myself what the reported HAML goodness is all about, so I spent a little time to educate myself on the basics, and then I took to converting one of my Rails erb files over to HAML. I’m posting the before and after results here as HAML examples to give one more convincing example of how much easier it is to read and write HAML views than the more noisy erb you-must-close-every-tag format.
At the end of this article, I list a few links to resources I found useful as I was getting started with HAML in my Rails application development.
Before (erb):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<div class="page-heading"> <h1>Quote: <%= @quote.formatted_id ></h1> </div> <= render 'form' > <div id="tabs"> <ul> <li><a href="#devices-tab">Devices</a></li> <li><a href="#options-tab">System Options</a></li> <li><a href="#reports-tab">Reports</a></li> <li><a href="#notes-tab">Notes</a></li> </ul> </div> <div id="devices-tab"> Devices Page</div> <div id="options-tab"> Options Page</div> <div id="reports-tab"> Reports Page</div> <div id="notes-tab"> Notes Page</div> <script type="text/javascript"> $(document).ready(function() { $( "#tabs" ).tabs(); }); </script> |
After (HAML):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
.page-heading %h1 Quote #{@quote.formatted_id} = render 'form' #tabs %ul %li %a{:href => "#devices-tab"}Devices %li %a{:href => "#options-tab"}System Options %li %a{:href => "#reports-tab"}Reports %li %a{:href => "#notes-tab"}Notes #devices-tab Devices Page #options-tab Options Page #reports-tab Reports Page #notes-tab Notes Page :javascript $(document).ready(function() { $( "#tabs" ).tabs(); }); |
More HAML examples and links
Some Thoughts on HAML by Rob Conery
Daniel Fischer says HAML is beautiful poetry
another HAML Cheat Sheet on http://cheat.errtheblog.com
HAML questions on StackOverflow
http://www.rubyinside.com/haml-a-new-view-template-language-for-rails-235.html
Learn more about HAML on Wikipedia
HAML Videos
A video on HAML by Jason McCay from BarCamp Birmingham 2008-04-12
A HAML video from John Schult at merb day Atlanta 2008-12-06
Hey Matt,
Nice article.
You may also want to add http://html2haml.heroku.com to you reference list it is a way to convert erb to haml via the web.
can i get an example of the simple project using haml and sass?
Sorry, I cannot share the code.