Haml doesn't like javascript

Written by on Feb 19 2008

We have been going back and forth on “Haml”:http://haml.hamptoncatlin.com/ lately. There is no doubt that it is nice, it is short, tight and does a lot for you. At the root of haml is yaml, a format for storing data. In yaml (and consequently in haml) indenting is very important, and for this reason haml and javascript go together like oil and grape jelly. To haml, javascript is just plain text, which means to make haml happy all of your lines of javascript code must allign vertically. Here is an example: In html:

In haml:

%script{:type => “text/javascript”, :charset => “utf-8”} //<!–[CDATA[ jQuery(function(){ tog(“#forgotpasswordclicker”, “#loginform”); tog(“#forgotpasswordclicker”, “#forgotform”, forgottext); }); function forgottext(){ if (jQuery.trim(jQuery(“#forgottext”).html()) == “forgot”) jQuery(“#forgottext”).html(“remember”); else jQuery(“#forgot_text”).html(“forgot”); } //]]–>

A few things to note: # Javascript becomes much harder to read and write. # Textmate does not do syntax highlighting on the javascript. # The haml requires the jQuery.trim() method in the first line of forgot_text() function because with haml you need to add a bunch of white space: # If you’re going to output ruby variables into your javascript, it only gets worse. html:

I forgot my password or username


%span I %span#forgottext forgot my %a{:href => “javascript:void(0)”, :id => “forgotpassword_clicker”} password or username

There are things one can do to overcome this: # One could use :plain and then indent javascript. * The problem is doing that means one can’t render ruby variables to the javascript. # Write unobtrusive javascript. * Nice, but I really like to cheat. # Put all of your javascript in partials * Sorry, I just threw up in my mouth a little bit. # Patch haml. * Let me know when you do this. # Stop writing javascript. * I also think you should start using frames again. # If anyone knows of any other way around this, please let me know. We’d love to use haml, but not at this price. # Don’t use haml. * Our choice for today. Although we will review this regularly.


Hi I'm Steven,

I wrote the article you're reading... I lead the developers, write music, used to race motorcycles, and help clients find the right features to build on their product.

Get Blog Updates