Compile the client part of a template template in ember using HTMLbars

I created a CMS where you can create HTML templates for creating templates. Templates must be compiled on the client side, and there is a component that should display the template. I set the component template property to a function that returns a compiled template using HTMLBars.

import Ember from 'ember';

export default Ember.Component.extend({
  content: null,
  template: function () {
    return Ember.HTMLBars.compile(this.get('content.template'));
  }
}

I have included the ember-template compiler in my Brocfile.

app.import('bower_components/ember/ember-template-compiler.js');

also tested

app.import('bower_components/ember-template-compiler/index.js');

But the template is never displayed.

+4
source share
1 answer

It should be a property, and it should be on the component layout, but it will be evaluated only once, so updating the content will not rebuild the template.

http://emberjs.jsbin.com/vayereqapo/1/edit?html,js,output

Ember.Component.extend({
  content: {template: 'Hello'},
  layout: function () {
    return Ember.HTMLBars.compile(this.get('content.template'));
  }.property()
});

Rerender :

App.FooBarComponent = Ember.Component.extend({
  content: {template: 'Hello'},
  foo: function(){
    var self = this;
    Em.run.later(function(){
      self.set('content.template', 'Bye');
      self.rerender();
    }, 3000);
  }.on('init'),
  layout: function () {
    return Ember.HTMLBars.compile(this.get('content.template'));
  }.property('content.template')
});

http://emberjs.jsbin.com/qebuxuxasu/1/edit

+4

All Articles