Previously, I've worked on large javascript applications that make tons of manipulations to the DOM manually or store lots of HTML as a string. When these pieces of code pieces grow they become a huge pain to change and maintain. Javascript templating to the rescue! I've been working with both jquery-tmpl.js and handlebars.js and want to offer a simple comparison.
First, a little background. Jquery-tmpl is the official jQuery templating plugin as of October, 2010 and is hosted in jQuery's github. Handlebars is an extension to Mustache.js with more features. A mustache.js template is a Handlebars template. Mustache.js is a templating system that has been implemented across many languages. You can find more about these plugins at the following links.
jquery-tmpl.js - http://www.borismoore.com/2010/10/jquery-templates-is-now-official-jquery.html (and you can take a look at jQuery docs).
handlebars.js - http://handlebars.strobeapp.com/ (To download handlebars.js go to this here https://github.com/wycats/handlebars.js/downloads)
mustache.js - http://mustache.github.com/
In the examples I'll be writing my templates in a script tag with the type set to something that the browser cannot recognize (x-jquery-tmpl or x-handlebars-template). You can still write the templates as html inline to javascript, but I don't prefer this method for anything but the simplest templates for the reasons stated above. Also, Jquery-tmpl will be the first block of code, and Handlebars will be the second block of code.
Read the rest of this post »