Handlebars.js is a JavaScript templating library. Web apps are using JavaScript to create dynamic interfaces now more than ever before, and that’s not a trend that will change any time soon. DOM manipulation is great for simpler JavaScript apps, but what do you do when you’re changing huge chunks of the document with each change of the view? That’s where JavaScript templating has a critical role to play.
4. Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
Templating Engines
Seperating business logic
from presentation
Caching system
5. We generally use server side templates
Dynamic Interface
HTML or JSON ?
Cleaner and simpler
Decoupled code
JQote,mustache,jTemplates,nano,
handlebars etc.
Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
Client Side Templates
6. Started by Yehuda Katz
Built upon {{ Mustache }} (Logic-less templating)
Compiler built with Javascript
Input : Handlebar expressions and HTML
Output : Javascript function
template = Handlebars.compile($('#myID').html());
This function then takes Data and returns HTML string.
appendText = template(data);
Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
Handlebars.js - Basics
10. Global block helpers
{{#if block}} {{else}} {{/if}}
{{#unless block}}{{/unless}}
(false, 0, undefined, null, "" or [])
{{^expression}} just like {{#unless}}
but not on zero
{{#each block}}{{/each}}
{{#with block}}{{/with}}
Examples
Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
11. Can be used in any context
Handlebars.registerHelper
options.fn, inverse
Examples
Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
Custom Expression Helpers
12. Sub Templates
{{> partialName}}
Render a section within a larger template
Handlebars.registerPartial()
Example
Presenter: PRABHAT KUMAR RAI,
Mindfire Solutions
Handlebars.js - Partials