Haml Tips and Tricks

I use Haml for my templates, and I mostly love using it and find it very intuitive. However, there are a few things that seem to trip me up on a regular basis, and I constantly find myself googling for the same Stack Overflow posts … so now I’m doing something I should have done ages ago and just posting them here for easy reference. Hopefully they might even help somebody else.

Don’t escape my HTML

By default, Haml escapes HTML, because in most cases this helps protect from XSS – but sometimes it’s my HTML and I know it’s safe, so I don’t want it escaped – or maybe I really just wanna let the bad guys hack me. For this, use an exclamation mark (!).

!= "I like my html UNESCAPED!!"

Thank you Haml Docs.

Whitespace removal

Ever want to do a link followed by a comma? You know, like that? Only if you do that in Haml:

%a know
, like that?

It comes out with a space: You know , like that? But you can fix it with the greater than operator, >, which removes all whitespace surrounding the tag.

Thank you, Haml Docs and Heikki on Stack Overflow

, like that?

Inline, Indented Javascript

This is almost always a bad idea, but if you find yourself forced to do it, use the javascript filter.

  function myFunction() { 
    // do some stuff

Thank you Haml Docs and bcoughlan on Stack Overflow.

The Haml docs contain a lot of other useful stuff, but these are at least two things that I am tired of looking up!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>