- Notifications
You must be signed in to change notification settings - Fork2.1k
Minimal templating on steroids.
License
handlebars-lang/handlebars.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Handlebars.js is an extension to theMustache templatinglanguage created by Chris Wanstrath.Handlebars.js and Mustache are both logicless templating languages thatkeep the view and the code separated like we all know they should be.
Checkout the official Handlebars docs site athttp://www.handlebarsjs.com and the live demo athttp://tryhandlebarsjs.com/.
See ourinstallation documentation.
In general, the syntax of Handlebars.js templates is a supersetof Mustache templates. For basic syntax, check out theMustachemanpage.
Once you have a template, use theHandlebars.compile method to compilethe template into a function. The generated function takes a contextargument, which will be used to render the template.
varsource="<p>Hello, my name is {{name}}. I am from {{hometown}}. I have "+"{{kids.length}} kids:</p>"+"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";vartemplate=Handlebars.compile(source);vardata={"name":"Alan","hometown":"Somewhere, TX","kids":[{"name":"Jimmy","age":"12"},{"name":"Sally","age":"4"}]};varresult=template(data);// Would render:// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>// <ul>// <li>Jimmy is 12</li>// <li>Sally is 4</li>// </ul>
Full documentation and more examples are athandlebarsjs.com.
Handlebars allows templates to be precompiled and included as javascript code rather than the handlebars template allowing for faster startup time. Full details are locatedhere.
Handlebars.js adds a couple of additional features to make writingtemplates easier and also changes a tiny detail of how partials work.
Block expressions have the same syntax as mustache sections but should not be confused with one another. Sections are akin to an impliciteach orwith statement depending on the input data and helpers are explicit pieces of code that are free to implement whatever behavior they like. Themustache spec defines the exact behavior of sections. In the case of name conflicts, helpers are given priority.
There are a few Mustache behaviors that Handlebars does not implement.
- Handlebars deviates from Mustache slightly in that it does not perform recursive lookup by default. The compile time
compatflag must be set to enable this functionality. Users should note that there is a performance cost for enabling this flag. The exact cost varies by template, but it's recommended that performance sensitive operations should avoid this mode and instead opt for explicit path references. - The optional Mustache-style lambdas are not supported. Instead Handlebars provides its own lambda resolution that follows the behaviors of helpers.
- Alternative delimiters are not supported.
Handlebars has been designed to work in any ECMAScript 3 environment. This includes
- Node.js
- Chrome
- Firefox
- Safari 5+
- Opera 11+
- IE 6+
Older versions and other runtimes are likely to work but have not been formallytested. The compiler requiresJSON.stringify to be implemented natively or via a polyfill. If using the precompiler this is not necessary.
In a rough performance test, precompiled Handlebars.js templates (inthe original version of Handlebars.js) rendered in about half thetime of Mustache templates. It would be a shame if it were any otherway, since they were precompiled, but the difference in architecturedoes have some big performance advantages. Justin Marney, a.k.a.gotascii, confirmed that with anindependent test. Therewritten Handlebars (current version) is faster than the old version,with manyperformance tests being 5 to 7 times faster than the Mustache equivalent.
Seerelease-notes.md for upgrade notes.
SeeFAQ.md for known issues and common pitfalls.
- Assemble, by@jonschlinkertand@doowb, is a static site generator that uses Handlebars.jsas its template engine.
- Cory, by@leo, is another tiny static site generator
- CoSchedule An editorial calendar for WordPress that uses Handlebars.js
- dashbars A modern helper library for Handlebars.js.
- Ember.js makes Handlebars.js the primary way tostructure your views, also with automatic data binding support.
- Ghost Just a blogging platform.
- handlebars_assets: A Rails Asset Pipeline gemfrom Les Hill (@leshill).
- handlebars-helpers is an extensive librarywith 100+ handlebars helpers.
- handlebars-layouts is a set of helpers which implement extendible and embeddable layout blocks as seen in other popular templating languages.
- hbs: An Express.js view engine adapter for Handlebars.js,from Don Park.
- koa-hbs:koa generator basedrenderer for Handlebars.js.
- jblotus createdhttp://tryhandlebarsjs.comfor anyone who would like to try out Handlebars.js in their browser.
- jQuery plugin: allows you to useHandlebars.js withjQuery.
- Lumbar provides easy module-based template management forhandlebars projects.
- Marionette.Handlebars adds support for Handlebars and Mustache templates to Marionette.
- sammy.js by Aaron Quint, a.k.a. quirkey,supports Handlebars.js as one of its template plugins.
- SproutCore uses Handlebars.js as its maintemplating engine, extending it with automatic data binding support.
- YUI implements a port of handlebars
- Swag by@elving is a growing collection of helpers for handlebars.js. Give your handlebars.js templates some swag son!
- DOMBars is a DOM-based templating engine built on the Handlebars parser and runtimeDEPRECATED
- promised-handlebars is a wrapper for Handlebars that allows helpers to return Promises.
- just-handlebars-helpers A fully tested lightweight package with common Handlebars helpers.
Have a project using Handlebars? Send us apull request!
Handlebars.js is released under the MIT license.
About
Minimal templating on steroids.
Topics
Resources
License
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.