5

I'm trying emblem.js right now. It's a really good wrapper of Handlebars to write templates. However, the docs are a bit ember.js and handlebars.js dependent. I want to use Emblem.js without Ember, but there is no real explanation on how to compile the template.

So can we use emblem.js without ember (or better, without Handlebars dependency)? The way I'm doing it right now, I have this function to render the template:

function render(target, tmpl, data) {
    var source   = tmpl.html();
    var template = Emblem.compile(Handlebars, source);
    var result = template(data);

    target.html(result);
}

Is that the correct way to compile Emblem? It works, but I have a gut feeling that there's a better way to do that. In Handlebars, the compile line is quite similar:

var template = Handlebars.compile(source);

Thanks for the answers.

Henson
  • 5,563
  • 12
  • 46
  • 60

1 Answers1

7

You're actually doing it correctly.

Emblem compiles down to a Handlebars runtime, and therefore needs you to specify which Handlebars variant you'd like to compile to. In your case, because you don't want Ember functionality, you'll want

Emblem.compile(Handlebars, source);

If you wanted Ember support, you'd need to use the Ember.Handlebars variant

Emblem.compile(Ember.Handlebars, source);

I'll definitely make sure to add this stuff to the docs.

  • Thanks! Please do. The docs don't really explain this bit, and I'm sure there are other people who are confused about how to do this, too. – Henson May 16 '13 at 17:42
  • I've been working with Emblem for long now on an Ember project, but in my new company they use Backbone, that your answer means that definitively Emblem can be used without strictly having to use Ember.js ? – htatche Mar 13 '14 at 10:04
  • @htatche yes, just use `Emblem.compile(Handlebars, source);` rather than the Ember.Handlebars variant. – Alexander Wallace Matchneer Mar 13 '14 at 14:43
  • Yup, I did it and it works fine, running now on Backbone through RequireJS. Thanks, I'm switching everyone here to Emblem now ! – htatche Mar 13 '14 at 15:37