someone
someone

Reputation: 1

How to render precompiled Handlebars template in Ember.js?

Using Handlebars directly:

var template = Handlebars.compile("<div>template source</div>");
var html = template();

But when using a precompiled template with Ember.js, this wont work:

var template = Ember.TEMPLATES["myTemplate"];
template(); // throws an error

Also if a template is compiled using Handlebars.compile, properties wont be readed properly from Ember objects since get is not called.

Upvotes: 0

Views: 775

Answers (2)

someone
someone

Reputation: 1

Templates compiled with the Ember compiler are hard to call in isolation.

Like normal Handlebars templates they are functions, but instead of doing string concatenation, they send their output to a Ember.RenderBuffer (calling push).

Mocking that object is not enough, since they also need a reference to a Ember.View (which normally calls them), and the property syntax {{mustache}} internally creates a Ember._HandlebarsBoundView (as stated in http://emberjs.com/api/classes/Ember._HandlebarsBoundView.html).

A solution is to use the normal Handlebars compiler, and passing properties to the resulting function as a plain JS object.

Upvotes: 0

Duncan Walker
Duncan Walker

Reputation: 2201

You render a template using Em.Handlebars.compile('template stuff here') (not Handlebars.compile) because Ember extends the Handlebars library to automatically use the get() method, per the Ember.Handlebars docs here.

Upvotes: 1

Related Questions