Cheating on ERB with HAML
Posted by hardwarehank, Wed Feb 07 20:30:50 UTC 2007
Flash!! Zoom!!
!!!
%html
%head
%link{ :rel => "shortcut icon", :href => "/favicon.ico", :type => "image/x-icon"}/
%title= "Wallpaper Love Factory :: "+controller.controller_name.capitalize
= stylesheet_link_tag 'wallpaper'
%body
.header#header
= render :partial => 'layouts/header'
.menu#menu= render :partial => 'layouts/menu'
.content#content= yield
It’s so easy! Anyone can do it. HAML is sweeping the rails world like a wildfire of chicken, and everyone wants the biggest piece. I decided to try it last night on my new wallpaper site, and found that it’s extremely awesome to work with. As you can see above, I used it for my application.haml layout. Here’s a breakdown of wtf is going on up there:
- !!!: That pesky DOCTYPE crap line that everyone hates and googles all the time.
- %tag: You use % to denote a new tag, and close it with indentation, like Python.
- %link{:rel => “shortcut icon”}: All the tag attributes are just a ruby hash!
- = stylesheet_link_tag ‘wallpaper’: Use the = to evaluate Ruby and display it.
- .header: Makes a div with a class of ‘header’.
- .header#homie: Makes a div with a class of ‘header’ and an id of ‘homie’.
- .menu#menu= render :partial => ‘layouts/menu’: Should be obvious by now. Renders a partial within a div with class ‘menu’ and id ‘menu’.
I found that it’s ridiculously easy to write views now. But what about loops?
- @wallpapers.each do |w|
.image[w]
= w.name
# Makes:
<div class='wallpaper image' id='wallpaper_4'>
Manatee
</div>
Holy dog crap! You don’t even have to use end anymore! It’s all Python style! Now, if you want to have even more fun, you can make self closing tags like img!:
%img{:src => 'your_face.jpg'}/
# Produces
<img src='your_face.jpg'/>
This is probably a bad example because the HAML is actually longer than the result, but who cares!!!
For more tricks and stuff, check out the HAML reference and tutorial.

Blog Posts
May 05, 2008 @ 04:04 PM
Just now learning Rails. Today was my first time using HAML. Very nice. Also easy to install.