Skip to content


Subversion checkout URL

You can clone with
Download ZIP


haml templates... #50

christopherhein opened this Issue · 11 comments

4 participants


I am really loving what you have done with this gem, it's a pretty awesome helper for anyone getting into backbone... Although it would be really cool if this could also output the templates using HAML Assets. I'm a huge fan of the way haml is written so much as I've almost forgotten how to do simple things in raw html :p.


Im getting into backbone just about now and I was thinking I was going to have the same problem but check this gem: I think it helps..


Mustache is cool although for some reason I'm stil not it's biggest fan, compared to using HAML, I think it's style just suits be better... to each his own in my book. Although if you are interested I have a fork of this gem, I'll be putting in a pull request after I feel it's solid (and I add generators for Jasmine BDD spec) but anyways my fork adds a check to see if you have Rails.config.app_generators.javascript_template_engine :haml which is similar to what haml-rails does where when the gem is used it adds Rails.config.app_generators.template_engine :haml and when it sees javascript_template_engine :haml it will render haml views instead of the HTML versions this gem currently has. To actually get the javascript_template_engine :haml variable you can manually set it in your application.rb OR... you can check out my fork of HAML_Assets which will register that variable for you when you have it installed... just like HAML-Rails does, I've already put a pull-request in for this, pull request And have it working great on a local app I'm building using both my forks.

As I said earlier, I plan on putting in a pull request, but I want to get it checking if you are using Jasmine, and if so have it create base jasmine specs for you to play with thus replicating what a rails generator does ie Scaffold creating a view, model, controller, route, and tests :) Untill that point check out my fork if you'd like to do some testing I'd love to get some input.


100% with you Chris


@christopherhein I would love to try using your branch. Are there any special instructions needed to get it compiling haml files into jst.ejs, or should I just refer to the HAML_Assets docs?


Add the requirements to the Gemfile

gem "haml-rails"
gem "jquery-rails"
gem "haml_assets", git: "git://", branch: "develop"
gem "ejs"
gem "haml", git: "git://", branch: 'form_for_fix'
gem "rails-backbone", git: "git://", branch: "develop"

If you want to test Backbone with jasmine specs my develop branch will create basic specs for you as well if you are you will need:

gem "jasmine"
gem "jasmine-core"

Other then that you just need to rename your template files to .jst.ejs.haml that is if it is an existing project but if is a new one the generators should create the files for you.

I'm currently using it this setup on a site that I'm still working on, I have yet to find any bugs, that I haven't fixed... Let me know if you need any extra help getting it all running, as well if you find any bugs that would be greatly appreciated.


@christopherhein I got it working and so far, no problems.

I tried using haml's html2haml command, but found that it was replacing <%= ... %> and <% ... %> tags with erb syntax. However, html2haml --no-erb ... leaves those erb tags in place, which is desirable since those are actually ejs blocks for backbone templates. Here's the bash shell script I used for the conversion.

A couple other notes:

rails-backbone requires ejs and jasmine requires jasmine-core, so you can trim the required gems down by two

You'll probably get more users/testers if you include the instructions above on the front page of your branch. I'd be happy to help you write up some of the documentation if you're short on time.


@christopherhein I've run into a bug with the haml templates not rendering out on Heroku. I was able to reproduce the error on my development machine by running rake assets:precompile--the result is that jst.ejs.haml templates print out as the haml text instead of rendering out properly.

I found a workaround with config.assets.compile = true in my production.rb. This enables fallback to the asset pipeline when a precompiled asset is missing, and isn't ideal for production. That workaround only works so long as assets aren't precompiled at all. Any thoughts on what I could be missing?

Edit: It looks like Rails 3.1 asset fingerprinting was causing the problem. I used the Digestion gem to disable fingerprinting for my Backbone template files. I added this to my application.rb:

 # Exclude specific assets from fingerprinting (use a path, glob, regex or proc)
 config.assets.digest_exclusions << "/backbone/templates/*"

Which works for the default backbone-rails install path (app/assets/javascripts/backbone/templates). Digestion can be integrated into the gem and, according to its homepage, is also necessary for Rails 3.1 to play nicely with some other javascript libraries.


I personally have yet to try my app in a production mode so unfortunately I have yet to try it... but I will try to spend sometime tonight and recreate what you've run into there has to be some better solution then having to use another gem, although if it works then its not too bad.

About documentation I've been a bit wrapped up with a few other projects lately so sorry for not getting back yet, I have a bit of time I'll try to get some good documentation in for other people, and let you know when I get that setup and if you wouldn't mind adding your two cents we can get something solid for other people.

Thanks for keeping me upto date with your progress!


Totally understand being busy, no worries.

I believe the asset fingerprinting issue is that Rails 3.1 will convert a file like edit.jst.ejs.haml to edit-123456.jst.ejs, which the view won't know how to link to, so it falls back to using edit.jst.ejs.haml, treating it like an ejs file. That's just my theory, though, and I didn't have time to verify this digging through the generated asset files.

I'd be more than happy to help with documentation--let me know when you'd like to get that up.


@christopherhein Does your rails g backbone:scaffold produce haml files? It still generates jst.ejs files for me.


Just thought I'd update with my experiences for anyone considering using haml with backbone-rails. There's one caveat with using a server side haml compiling--haml supports Rails/Ruby syntax, but this has limited utility with jst templates, which are rendered client side.

Example of what I'd do in Rails Haml

- if var
   "#{var} and some text"

How I'd have to do this with for a Haml JST template:

<% if (var) { %>
  #{var} and some text
<% } %>

It's not too bad, but I wouldn't say it's readable. Ruby statements work with - and = at the beginning of the line, but since they're evaluated server side, they can't reference client side variables. I've very rarely used Ruby code in my JST HAML templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.