Last Updated on February 28, 2022 by cwl

The Depot project uses an “out-of -the-box” approach to creating Rails views. This is a good idea for a book and code that is geared toward learning about Ruby and Rails. But working with HTML tags can get awfully tedious, what with having to remember to add closing tags to everything. Sure a lot of code editors can do that for you, but I get tired of looking at HTML all day. Enter HAML – HTML abstraction markup language – it’s a whitespace-based DSL that really cleans up your view code. Best shown by an example, here’s the HTML for a Bootstrap 4 navbar. Would you rather look at this all day:

or this:

OK, OK, none of the above, but really, HAML is much easier to work on, trust me.

So, is there an efficient way to convert HTML to HAML? But of course – the haml-rails gem has that capability baked in.

Step one: add the gem to your project’s Gemfile, and then:

 bundle install 

Step two: You now have a new Rake command available. From the project’s root directory:

 bundle exec rake haml:erb2haml 

erb2haml creates a new set of .haml files. Pull them into your editor, check for any conversion errors, remove (maybe save in another location) your original .erb files and commit the changes.

The Depot views converted without error, which is what I expected since nothing exotic was added to the original views. More elaborate HTML elements might cause some breakage, although I was converting Bootstrap 4 components without any difficulty when I was updating my web site.

That’s it. The results are available at the GitHub repo. Cheers!