I want to dynamically compile (and then render) a HTMLBars template at runtime, on the client in Ember. How can I do this?
相关问题
- Angular: ngc or tsc?
- Google Test - generate values for template class i
- Template Specialization for Private Types
- Where is the implementation of included C++/C head
- zero length variadic expansion of ill-formed call
相关文章
- C++ Template specialization to provide extra membe
- C++: How to use unnamed template parameters in cla
- Templates, Function Pointers and C++0x
- Detect if C++ lambda can be converted to function
- How to use autofocus with ember.js templates?
- Including dependencies with ember-cli
- php module does not compile. Does not recognize “s
- Pass a reference of the clicked DOM element to the
Building off of Kingpin2K's answer to Compile template client side in ember using HTMLbars:
For some background, it might be useful to refer back to Compiling Templates with Ember 1.10. We'll still need to load
ember-template-compiler.js
. Addto your
ember-cli-build.js
.Then you can write a Component like this:
This solution will likely break in future relases of Ember, depending on how the Ember Template compilation process changes with the advent of Glimmer 2.
Since Ember 2.10 is now using Glimmer, things might be a bit tricky here. In order to compile a template, you need to include
ember-template-compiler.js
to your application. I'd recommend usingember-browserify
andember-source
.In your controller, import the compiler as the following.
As tested, your content can contain anything from Ember helpers to your custom components, even your action bindings.
e.g.
Now, let's do the magic in your template by using
{{partial}}
helper.This method works in Ember 2.13 without deprecation warnings, it should work in future updates. Please note that
Ember.TEMPLATES
is global variable and the engine seems to cache it somehow, so do not reassign new values to the existing one.Since Ember 2.13+ (without bower by default) you need to add in your ember-cli-build.js:
For Ember version prior to 2.10 you need to include it via
bower
(also on ember-cli-build.js)And on the code you need to:
In the hbs file call:
Or you can make a component like this:
Based on solution of another answer https://stackoverflow.com/a/37345099/6505594
I'm currently on Ember-2.9.x and I brought in the latest handlebars with my
bower.json
:And then added it via my
ember-cli-build.js
file:This has worked for my
typeahead
component and I don't see any reason why this won't work when upgrading to Ember-2.10 with Glimmer2.