HAML : Using variable in JavaScript block

February 10th, 2014 § 0 comments § permalink

While I was getting started with HAML templating, everything went fine until I need to use a HAML variable inside a JavaScript block. I hope you may familiar with JavaScript block in HAML like below.

:javascript
  var a = 10;
  console.log(a);

But what if that variable a is a HAML variable and I need to useĀ  inside the :javascript block? I had no idea how to do it, so went for some googling. The result yeilded was pretty amazing and easy. I just need to interpolate the variable as we do string interpolation in ruby.

:javascript
  console.log(#{a});

That’s it you are done. Now you are using a HAML variable inside the :javascript block.

Pretty easy right?