Skip to content
Daniel Kehoe edited this page May 8, 2012 · 19 revisions

Haml and Rails

by Daniel Kehoe

Last updated 9 April 2012

Using Haml with Rails. A guide for developers using the example apps from the RailsApps project. Others may find it helpful as well.

About Haml

Haml is a popular alternative to the default Rails templating language (“ERB”).

ERB is a templating system that embeds Ruby into an HTML document, similar to ASP, JSP and PHP. It preserves the syntax of HTML and allows Ruby code to be embedded within a pair of <% and %> delimiters. Haml eliminates the <% and %> delimiters and uses identation to mark the beginning and end of markup so that closing tags are not required.

Haml Advantages and Drawbacks

Haml is intended for “elegant” markup, eliminating the repetitive tags of HTML and integrating conditional logic succinctly. It’s easier to read.

Indentation is significant in Haml, which makes for well-structured code, but failures resulting from indentation errors are annoying when using Haml. An IDE that provides auto-indenting can make Haml easier to use (see HAML in RubyMine).

If you’re working on a team, you may find designers are more comfortable with ERB as it more closely resembles familiar HTML. And Haml will break if everyone on the team is not using tabs or spaces for indentation consistently.

Finally, for someone new to Rails, Haml is one more new thing to learn, which may not be welcome when there’s so much to learn already.

Despite these drawbacks, we use Haml for the RailsApps examples because it eliminates superfluous markup and makes view code much easier to read.

Setting Up Haml

Here are instructions for adding Haml to an existing Rails project. You’ll need extra gems in the Gemfile for Haml:

gem 'haml-rails', '>= 0.3.4', :group => :development

Run the gem bundler command:

$ bundle install

With the haml-rails gem, there is no need to modify the application.rb file to accommodate Haml. Any time you generate a controller or scaffold, you’ll get Haml instead of ERB templates. And when your Rails application loads, Haml will be loaded and initialized.

Syntax Highlighting for Haml

You’ll want to add Haml syntax highlighting to your text editor. There are several versions of TextMate bundles for Haml. The most current is the haml-textmate-bundle from Philippe Huibonhoa. TextMate bundles will work with the popular Sublime Text 2 editor as well. If you are using vim, you can install vim plugins for Haml.

Converting HTML to Haml

Most Rails examples and tutorials use ERB. But it is easy to convert ERB to Haml using the Html2Haml website. Paste your HTML or ERB code into a text field, click convert, and you’ll have clean Haml code, ready to paste in your app views.

Rails Example Application Using Haml

If you’d like to start a new project with Haml, you can generate a Rails application set up for Haml. Use this command to create a new Rails application:

rails new myapp -m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb

The application template will offer you a choice of Haml or ERB, give you options for a CSS front-end framework such as Twitter Bootstrap, and create a default application layout using HTML5 (see the article HTML5 Boilerplate for Rails Developers). It creates a simple starter app.

The Rails application templates from the RailsApps project offer a variety of other starter applications with Haml as an option.

Creating Haml Applications by Default

The rails new command creates a new Rails application with a default application layout using ERB. If you want to use Haml for every Rails application you build, you can set options for the rails new command in a .railsrc file in your home directory. Here’s how to set up a .railsrc file to use the rails3-haml-html5 template when you create a new Rails application:

# ~/.railsrc
-m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb

Using Haml in an Application Template

If you’d like to create an application template for a starter app, you can take a look at the haml recipe from the rails_apps_composer repository.

Credits

Brook Riggio wrote a helpful blog post on Haml by Default in a New Rails 3.2 App and initiated work on the rails3-haml-html5 template.

Daniel Kehoe wrote this article for the RailsApps project.

Was this useful to you? Follow me on Twitter:
rails_apps
and tweet some praise. I’d love to know you were helped out by the article.

Clone this wiki locally