javascripthtmlember.jshandlebars.jshtmlbars

Can HTMLbars be used without Ember.js?


I've been looking for a while now, and every time HTMLbars is mentioned Ember is involved. The problem is I want to migrate from Handlebars to HTMLbars, however my project has a custom framework. If all that HTMLbars does it make it so the content is returned as HTML elements, rather than a string, why is it Ember exclusive, and has anyone found a way to work around that exclusivity.


Solution

  • Yes, it can be used without Ember.js, you can analyze code in demos or you can dig into tests in HTMLBars repository.

    The most interesting fragment of code is:

    var template = compiler.template(templateSpec);
    var env = { dom: new DOMHelper(), hooks: hooks, helpers: {} };
    var scope = hooks.createFreshScope();
    
    hooks.bindSelf(env, scope, data);
    
    var dom = render(template, env, scope, { contextualElement: output }).fragment;
    
    output.innerHTML += '<hr><pre><code>' + JSON.stringify(data) + '</code></pre><hr>';
    output.appendChild(dom);