Building an Index Page with HAML

HAML is a simple templating language for Ruby. I finally decided to pick it up today.

I started with one of the simplest views in my rails app, an index page that lists all employees. Here’s the original code, in erb:

erb index

And here’s what I came up with in Haml:


The thing to notice here is that I had to create new divs for each bit of Ruby code.

 .col-md-3= e.full_name link_to "Edit", edit_employee_path(e) link_to "Delete", e, method: :delete

just won’t work in HAML. Which is fine, but it took me a little while to figure it out. Most of the tutorials I found didn’t really explain this, and the reference didn’t have any examples of how to put multiple lines of Ruby into a div either. Turns out it’s because you can’t, apparently.

Overall I’m finding HAML to be really nice.  It’s simple, quick to pick up (20 minutes for the basics, seriously), and cuts down on typing quite a bit.

Leave a Reply

Your email address will not be published. Required fields are marked *