5
votes

Is there any way to precompile Handlebars template, so that it will be aware about Ember.js helpers, like "action", "view", etc.?

The reason I'm asking is because I need to use Ember.js with a runtime-only build of the Handlebars (due to CSP in Google Chrome), and it seems that Ember.js does not completely support currently such scenario: https://github.com/emberjs/ember.js/issues/1303

1

1 Answers

4
votes

You can precompile an Ember Handlebars template using Ember.Handlebars.precompile (ember-handlebars/lib/ext.js#L104).

The basic strategy to use for precompilation outside of a browser (in node, for example) is:

  • Load handlebars.js
  • Load ember.js
  • Shim out a few browser APIs that Ember expects (this should not be necessary in 1.0, but it is necessary today).
  • template = Ember.Handlebars.precompile(string)
  • Ember.TEMPLATES[name] = Ember.Handlebars.template(template)

The ember-rails gem for Rails does this for you if you have an asset with the extension .handlebars.

The ember-rails gem also maintains a shim .js file that you can use to precompile in other non-browser environments (ember-precompiler.js).