Sitemap

Beautiful Markup

4 min readAug 18, 2017

--

An introduction to HTML Abstracted Markup Language

As a backend developer challenges abound in the early days of learning. There are so many new skills to pickup, documentation to read, reps to do and never enough hours in the day or wealth of focus to feel as though a dent is ever made in the back log. One of the biggest challenges for me has been balancing the desire to spread my focus with the understanding that a deep dive into a single framework will probably serve my future aspirations in a more meaningful way. It is through that train of thought that I have limited the learning of things outside the scope of necessary scope of Rails until I feel I have reached a certain level of proficiency. That changed after I discovered HAML.

One of the beautiful things about Rails is that it handles your views. Within it’s magical inner workings are a variety of tools that simplify many useful tasks. However, within the view, everything needs to be coded using HTML in concert with ERB(embedded Ruby). I have always felt that HTML was cumbersome to read and irritating to write (an opinion possibly shared or potentially scorned by the reader), which makes coding view after view extremely tiresome.

Enter HAML.

HTML Abstraction Markup Language or HAML is an alternative to HTML and ERB. It allows the developer to create highly readable markup that expresses the hierarchy of the DOM in a more literal way than HTML. It borrows syntax from CSS, it innately makes certain errors difficult or impossible to make and it’s even easy to learn(it only took me about two days to feel comfortable). However, the main selling point to me was no closing tags!

Let’s consider the following HTML block:

Press enter or click to view image in full size

The above is an example of a basic index page. It contains a series of standard tags, classes, an id, and a typical ERB block which renders all the items contained in example to their own divs. It is a lot of markup to accomplish a simple task. Let’s examine the same example in HAML

Press enter or click to view image in full size

It is immediately clear that the HAML example is cleaner, more concise and well represents the hierarchy of the page. HTML tags are represented with a leading ‘%’. CSS classes are represented with a leading ‘.’ and no quotations. CSS ID’s, which have a leading ‘#’, can also be added after tags or chained along after classes.

In the case of the common div tag, the %div can be omitted completely and is implied when using a class, such as on line 10 in the iterative block.

Get Ben Jacobs’s stories in your inbox

Join Medium for free to get updates from this writer.

Nesting within tags is represented as indentation. For example, everything that belongs inside the body tags in indented 2 spaces after it. Same for the %h3 and %p tags nested within the ‘.item’ div. The indentation puts both within the div.

Press enter or click to view image in full size

If the example looked like the above, the h3 would be nested within the ‘.item’ div but the p tag and it’s contents would not.

Again, my favorite part, there is no need for any closing tags. This allows for quicker composition with less overall code. HAML even goes so far as to do away with the need to end blocks of embedded Ruby adding readability and flow to all code blocks

HAML is not without it’s quirks. It’s opinionated indentation rules can stop you from making certain errors but can also lead errors which would cause no problems in HTML. However this has the unintended side effect of making HAML more uniform between developers. Whether this is a bug or a feature depends upon your perspective.

Additionally, I have found incorporating JavaScript into HAML to be quite the challenge though certainly not impossible. I would say, the project’s scripting needs may dictate whether the use of this option is preferred or more trouble than it’s worth.

Fortunately it is easy to dip a toe into the HAML pool without rewriting every ERB. Though they cannot co-exist in the same file they certainly can exist in the same project. This makes it easy to try it out without forcing your entire team to change styles all at once. However, if you do decide that you’d like to convert an entire project, definitely take a look at this gem for a quick script to make that process as painless as possible.

In the end I’m not convinced that HAML will always be the appropriate choice, but it is certainly a tool I enjoy using. It allows for view markup that is concise and readable and plays very nicely with Ruby code. It has sped up my flow and it sure is pretty to look at.

Resources:

HAML Docs

HAML Intro

HAML Gem

HAML to ERB Gem

--

--