Art Of Programming

musings by Dmytrii Nagirniak

Templating Done Easy - JavaScript HAML With Rails

HAML templating with no worries: native, precompiled HAML. Blazing fast and as easy to use as it can be.

UPDATE: this functionality moved into ruby-haml-js gem, please use it instead.

Recently I have been working on a Ruby gem that packs a set of common JavaScript libraries in one place so you can just reference those easily from Rails 3.1 app. But now I want to write a bit about a very handy tool that the gem includes. I needed to work on a Backbone app and, unfortunately, there was no very simple and easy way of using HAML markup for my client side templates. Main problems:
  1. I don’t want to embed the template into the document.
  2. The Underscore.js templating is ok, but it is too verbose for me (as most of others).
  3. The haml-js is great, but I do not want to bother precompiling the templates.
  4. I do not want to think about templating as another layer or component. I want it to just work.
Fortunately it was pretty easy solve these problems with the Sprockets and Rails 3.1.
So from now on you can just write normal HAML templates and consume those with no worries from JavaScript.
All you need to do, is to add pakunok gem to your Rails 3.1 Gemfile. This will give you templates as native JavaScript functions that you can use.
It’s better to see than hear
Feel free to head to the issues and provide some feedback or read more.