ianpetzer
ianpetzer

Reputation: 1838

Evaluate Ember template programmatically

I need to evaluate an Ember Template programmatically, so that I can generate the HTML that results by evaluating the Ember template within a supplied context.

I want to use this generated HTML to insert into a google maps InfoWindow (small modal that appears when you click on a marker.)

All of the variables will be unbound.

I have tried

Ember.TEMPLATES['templateName']( {context: 'suppliedHere})

but this expects a Ember.RenderBuffer to exist in the data.buffer object

Upvotes: 6

Views: 616

Answers (2)

briangonzalez
briangonzalez

Reputation: 1616

Here's another solution, inside your Ember view/component:

var viewClass = Ember.View.extend({ templateName: this.get('contentTemplate') });
var view = this.createChildView(viewClass);
var html = view.renderToBuffer().buffer;

Upvotes: 0

Sebastian Seilund
Sebastian Seilund

Reputation: 454

Ember uses Handlebars.js for templating.

If you have the template as a string you can use Handlebars directly:

var template = 'Hi {{name}}';
var context = {name: 'John'};
Handlebars.compile(template)(context); //returns "Hi John"

Upvotes: -2

Related Questions