Rails default templating language is ERB which embeds ruby code into a HTML document, but there is an alternative templating language available called HAML.
HAML(HTML abstraction markup language) provides an elegant syntax which is easy to understand and maintain, it shows the DOM hierarchy of a document.
In ERB we embed the ruby code between <% ruby code %> delimiters whereas HAML eliminates <% %> and use indentation for the beginning and end of markup so that closing tags are not required.
In order to use HAML in Rails project first we need to install HAML by simply adding Haml into Gemfile..
And run the gem bundler command:
$ bundle install
Now if we want to use Haml there is no need to modify the application.rb file. Because when Rails application loads, Haml will be loaded. Also if we generate a scaffold, we’ll get Haml instead of ERB templates.
Let’s convert some basic ERB into Haml
<span><%= @article.title %></span>
As you can see in Haml we write a tag by using the percent sign and then the name of the tag. Here after the tag name = is used, which tells Haml to evaluate Ruby code and then print the value. Haml automatically detects newlines in the return value and format the tag properly.
In the example above both the Haml line will produce the same code.As you can see class name is replaced with a . and id is replaced with #, also we have not used = so it will be interpreted as a normal string not as a Ruby code.
<div class='myclass'>Hello, World!</div>
.myclass Hello, World!
Here in Haml we have not used div tags as it will take default to %div.