Skip to main content

Switch to HAML

Published: Oct 6th, 2020

This is part of Joyful Rails, a list of recommendations to make developing your Rails app more productive and joyful.

In this article, we are talking about switching to HAML.

When

I recommend switching to HAML before investing significant effort into your HTML.

What

HAML is an HTML templating language. It makes it easier and more pleasant to write HTML templates.

Why

The default templating language for Rails is ERB (aka eRuby or Embedded Ruby). It is a simple way to embed Ruby code into text files using expression tags (<%= ‘<%= %>‘.html_safe %>) and execution tags (<%= ‘<% %>‘.html_safe %>).

HAML is a templating language that makes the HTML structure clear and easy to work with by using meaningful whitespace and eliminating unnecessary characters.

How

Add gem 'haml-rails' to your Gemfile and run bundle.

Replace all your ERB views with HAML views by running HAML_RAILS_DELETE_ERB=true rails haml:erb2haml.