hayley 365

home

haml vs slim

25 Jan 2014

2014-01-25 22:02:00 -0600

Well, tonight I learned basically what will be the final nail in the coffin for me ever switching to Slim.

For a brief history lesson, I switched to Haml when I started learning Rails the second time around. And though I haven't touched Rails since that time period (Sinatra and Middleman for me, thanks), I continue to use things like Haml.

I started using Slim a bit to test the waters. I was intrigued by its nicer syntax, because in Haml I was doing things like this:

%a{:href => "http://www.google.com", :class => "someclassname", :target => "_blank"}

In Slim, it was simply:

a href="http://www.google.com" class="someclassname" target="_blank"

Much nicer.

Over time, of course, I came to learn that both languages supported doing things like %a.classname (Haml) and a.classname (Slim), so that improved things in lots of cases.

Even with these things, Slim never quite caught on in my workflow. I didn't port anything in either direction. In other words, things I started in Haml stayed in Haml and things I started in Slim stayed in Slim.

Later, I discovered that Haml no longer needed the hash rocket syntax. I'll make a guess that this happened during the Ruby 1.9 upgrade. It's just something I happened to stumble upon in someone else's Ruby code.

So instead of:

%a{:href => "http://www.google.com", :class => "someclassname", :target => "_blank"}

You could do:

%a{href: "http://www.google.com", class: "someclassname", target: "_blank"}

It's not like it was a life changer, but it was even less typing.

And now, I discover that Haml has something almost as good as Slim.

Here's the Slim again:

a href="http://www.google.com" class="someclassname" target="_blank"

And here's the Haml I discovered tonight:

a(href="http://www.google.com" class="someclassname" target="_blank")

the real reason Slim never rocked my world

Slim and Haml have different ways of handling whitespace.

Haml basically applies whitespace after every tag.

Slim expects you to be much more explicit about when you actually do want the whitespace.

Here's the thing, I get burned by Haml's whitespace handling often enough that I couldn't even remember the syntax to fix it (more on that later). Whereas, I get burned by Slim's whitespace handling on a near daily basis.

In Slim, you end up having to padding sentences with trailing spaces, or prepending lines with extra space. All of these extra random spaces just offend my sensibilities. And of course, depending on how you've got your text editor configured, you might not even be able to tell if your whitespace is there or not.

The Haml thing basically burns me when I want to do things like have a link at the end of the sentence but not have the ending period be part of the link. By default you'd end up with an unwanted space between the end of the link and the period.

Frankly, I was far more likely to "solve" this by just rewording sentences so that the link wasn't the final bit of text.

Frankly, I can't even find the old way of how to fix this, but apparently the preferred method these days is to use succeed. Here's an example:

%p
  Here's some text and then a
  = succeed '.' do
    %a(href="#") link

I'm not crazy about that syntax, but like I said, Haml's handling of whitespace burns me far less often than Slim's.

And now that I've discovered that Haml has the parentheses syntax for declaring things in a much more HTML-like way, I doubt I'll be deliberately going after Slim for any new projects.

Frankly though, I'm not sure if I'll actually even start using the parentheses syntax or not. But it's nice to find out that it exists.