1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Using Haml with Sinatra

By

1 of 7

Using Haml with Sinatra

Haml is a markup language you can use in place of erb. Though erb is the "standard" since it comes with Ruby, Haml has a lot of benefits that's difficult to ignore. First, it's succinct and very easy to write. In erb with HTML or XML, there's a pretty high symbol to content ratio. Haml has very few symbols, only starting tags and very little to get between you and the content. This alone is a huge reason to use Haml over erb, it's just so much easier to read and write.

So what does Haml look like? Here's an HTML snippet in both HTML and Haml. First, the HTML document.

<div id='post'>
 <div class='title'>
    Title of the blog post
  </div>
   <div class='body'>
    This is the body of the blog post.
    It's quite long, and can be broken into
    many lines.
  </div>
</div>

As expected, it's quite heavy in markup and symbols. While this is normally expected with HTML and XML code, it doesn't have to be this way. It can look like this instead.

#post
 .title
    Title of the blog post
  .body
    This is the body of the blog post.
    It's quite long, and can be broken into
    many lines.

This is much cleaner for a number of reasons. First, there are no ending tags. Also, the only symbols used are at the beginning of the line. To nest tags, you simply indent the lines just like a Python program. This makes very clean markup that's easy to read and write.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Sinatra
  6. Using Haml with Sinatra

©2014 About.com. All rights reserved.