Did you know that HAML and RAILS doing really good together?

Before start why “Haml” is good with rails let me say something about “ERB”. ERB is the default template system for rails. We embed Ruby into HTML using ERB just like we do in ASP, JSP and PHP.

Following example will show how we can embed ruby in an html.erb file.

As you can see in the code segment, I have created a basic form in rails using a table layout. I have used the following delimiter to embed ruby within the html file.

when there is a huge code in an erb file, it is really annoying and I am like…

So I started using HAML instead of ERB.

HAML is a very attractive and popular alternative to ERB. HAML will eliminate repetitive tags in HTML. Ultimately it gives me a beautiful code where it is easy to read.

So, HAML is a beautiful, DRY, well-intended, clear markup

Indentation is the main key with HAML, which makes for well structured code. Sometimes you might feel unconformable when rails give indentation errors, anyway it won’t happen if you are so careful and you love this.

This is the above erb code using haml.

I think you can just see the difference even by looking into the code. I like using HAML and it is super easy to handle my view files in rails. Not only the readability, its focus on cleanliness and production speed.

for more information please refer the following link

This is how I add HAML into the project

  1. First add the GEM file.

you can use the above link to read about HAML.

or

2. Do a bundle install, run the following command in the terminal

3. Done!

Now anytime you create a controller after bundle install. Rails will generate view in the formal for HAML.

Move from HTML to HAML

After adding HAML i got issue initially, How can i convert my existing html.erb to html.haml. I use following link to convert my existing code. Pretty easy.

This is something I didn’t do. but if you need to convert the whole ERB project to a HAML project. you can use a shell script. See the following link for that.

or use the following gem to convert

Tutorial

Basics you want to know is mention bellow

Let’s see some examples, so you can get some idea.

ERB

HAML

When we add attributes into tags

ERB

HAML

If you want to add dive with a class “content”

Same thing with an ID

ERB

HAML

--

--

Developer ❤️ JS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store