Since Handlebars.js can have different expression / rendering logic in the template, these expressions are usually processed at runtime. For better performance and less dependency, templates can be precompiled before deployment. For example, here is a simple steering pattern:
<div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> </div>
And here is the precompiled output
(function() { var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; templates['test.handlebar'] = template(function (Handlebars,depth0,helpers,partials,data) { helpers = helpers || Handlebars.helpers; var buffer = "", stack1, foundHelper, functionType="function", escapeExpression=this.escapeExpression; buffer += "<div class=\"entry\">\r\n <h1>"; foundHelper = helpers.title; if (foundHelper) { stack1 = foundHelper.call(depth0, {hash:{}}); } else { stack1 = depth0.title; stack1 = typeof stack1 === functionType ? stack1() : stack1; } buffer += escapeExpression(stack1) + "</h1>\r\n <div class=\"body\">\r\n "; foundHelper = helpers.body; if (foundHelper) { stack1 = foundHelper.call(depth0, {hash:{}}); } else { stack1 = depth0.body; stack1 = typeof stack1 === functionType ? stack1() : stack1; } buffer += escapeExpression(stack1) + "\r\n </div>\r\n</div>"; return buffer;}); })();
More information about precompilation can be found here.
source share