2011-05-09
By Ivo Dancet

haml-js and jammit

Using haml-js and Jammit in a Rails app (with backbone.js)

Some time ago I submitted a small patch to Jammit to enable haml-js as a template language. Afterwards I have received several questions through github on how to use it. I hope this blog entry helps if you too have any trouble to set this up.

Aside from the haml-js stuff I also added some tips. You will need to read through the Jammit documentation to know how to set things up from zero.

[update] setting up Rails

In order to be able to use the patch you'll still need to use the gem through github instead of the official one. So add the following to your Gemfile: (thanks for pointing this out David Francisco)

gem "jammit", :git => 'git://github.com/documentcloud/jammit.git'
          

You should not use the fork I've made since I may delete it soon (the only purpose was to be able to create the patch, I certainly do not intend to keep it around).

Setting up Jammit: assets.yml

This is an example config/assets.yml file. You can also see how I organise my files here.

As you can see I have mixed the view files within app/views (and are thus like partials, which in fact, they are of course).

You can choose the template_extension. I think .jst.haml is perfect. I also follow Rails conventions and add an underscore as a prefix to the filenames.

You also need Haml as the template_function. The compiled file Jammit is going to deliver (and your Rails application will use) looks like this:

Of course you need to have the haml-js required to make things work. From here on you can get back to the Jammit documentation.

Using the templates with backbone.js

This is pretty straightforward:

UserView = Backbone.View.extend({
            template: JST["users/_show"],
            render: function() {
              $(this.el).append(this.template(this.model.attributes));
              this.addAll();
              return this;
            }
          })
          
blog comments powered by Disqus
About
This blog is written by Wouter Vancraeynest and Ivo Dancet and is about what we come across while working at our company by2.be, located in Bruges, Belgium. This can be anything related - though sometimes only remotely - to programming, testing, marketing, Ruby, Apple and other things we do and love ...
Recent posts
Optimizing Vim: tab style
haml-js and jammit
VimRoom
Hudson for a Rails3 project
Looking up Rails(3) routes
Horror story, part II
All tags
Jammit - SaaS - Rails - WriteRoom - Haml - rSpec - Vim - Ruby on Rails - rcov - Cucumber - humans - continuous integration - Hudson - Rails3 - testing - rSquery - haml-js - Bundler - rvm - backbone.js - Selenium
rSquery
Some time ago we made rSquery to be able to easily use jQuery matchers in Selenium projects. You may want to check it out on github. By now this project is not in active development anymore but older projects can still profit from it. (We still use it)