Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between handlebar.js and handlebar.runtime.js?

I found that handlebar.runtime.js has no compile method. So I downloaded not the right version to just use the template engine.

But what is the handlebar.runtime.js is for?

It would be nicer that Download: runtime-1.0.0 would be more unnoticeable on the download page?

like image 301
static Avatar asked Oct 02 '13 21:10

static


People also ask

What is the latest version of handlebars js?

Latest release (version 4.7. Use this version as a quick start, if you want to compile your templates in the browser.

What is the use of handlebar js?

Handlebars. js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces.

How handlebars js is different from mustache js?

Handlebars. js is an extension to the Mustache templating language created by Chris Wanstrath. Handlebars. js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be; Mustache: Logic-less templates.


1 Answers

Handlebars uses tags that look like {{this}} that can not be natively understood by the browser. In order for the browser to render these tags, they have to be compiled. Compilation can happen either before or after you load the page.

To speed things up, you can precompile your templates. More info at the handlebars site. If you do this, you only need to include the handlebars runtime script on your page. It's smaller than the full handlebars script because it doesn't need to worry about compiling templates. It assumes you have precompiled them.

When a template is compiled, it is converted into a function that, when called, will return real HTML wherein curly brace tags have been converted into values the browser understands.

For example, this...

<div class="entry">   <h1>{{title}}</h1>   <div class="body">     {{body}}   </div> </div> 

...will be converted into the following (as of June 2014) after being precompiled:

(function() {   var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; templates['test.hbs'] = template({"compiler":[5,">= 2.0.0"],"main":function(depth0,helpers,partials,data) {   var helper, functionType="function", escapeExpression=this.escapeExpression;   return "<div class=\"entry\">\n  <h1>"     + escapeExpression(((helper = helpers.title || (depth0 && depth0.title)),(typeof helper === functionType ? helper.call(depth0, {"name":"title","hash":{},"data":data}) : helper)))     + "</h1>\n  <div class=\"body\">\n    "     + escapeExpression(((helper = helpers.body || (depth0 && depth0.body)),(typeof helper === functionType ? helper.call(depth0, {"name":"body","hash":{},"data":data}) : helper)))     + "\n  </div>\n</div>\n"; },"useData":true}); })(); 

The important takeaway here is that at some point, the handlebars template has to be converted into this function so that real HTML can be generated. The handlebars runtime script doesn't contain the compiler thus making it smaller. And by precompiling your templates, there will be one less heavy step the JavaScript has to go through before rendering the page.

like image 198
rescuecreative Avatar answered Oct 07 '22 11:10

rescuecreative