1[](https://github.com/handlebars-lang/handlebars.js/actions/workflows/ci.yml) 2[](https://www.jsdelivr.com/package/npm/handlebars) 3[](https://www.npmjs.com/package/handlebars) 4[](https://www.npmjs.com/package/handlebars) 5[](https://bundlephobia.com/package/handlebars) 6[](https://packagephobia.com/result?p=handlebars) 7 8Handlebars.js 9============= 10 11Handlebars.js is an extension to the [Mustache templating 12language](https://mustache.github.io/) created by Chris Wanstrath. 13Handlebars.js and Mustache are both logicless templating languages that 14keep the view and the code separated like we all know they should be. 15 16Checkout the official Handlebars docs site at 17[https://handlebarsjs.com/](https://handlebarsjs.com) and the live demo at [http://tryhandlebarsjs.com/](http://tryhandlebarsjs.com/). 18 19Installing 20---------- 21 22See our [installation documentation](https://handlebarsjs.com/installation/). 23 24Usage 25----- 26In general, the syntax of Handlebars.js templates is a superset 27of Mustache templates. For basic syntax, check out the [Mustache 28manpage](https://mustache.github.io/mustache.5.html). 29 30Once you have a template, use the `Handlebars.compile` method to compile 31the template into a function. The generated function takes a context 32argument, which will be used to render the template. 33 34```js 35var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " + 36 "{{kids.length}} kids:</p>" + 37 "<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>"; 38var template = Handlebars.compile(source); 39 40var data = { "name": "Alan", "hometown": "Somewhere, TX", 41 "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}; 42var result = template(data); 43 44// Would render: 45// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p> 46// <ul> 47// <li>Jimmy is 12</li> 48// <li>Sally is 4</li> 49// </ul> 50``` 51 52Full documentation and more examples are at [handlebarsjs.com](https://handlebarsjs.com/). 53 54Precompiling Templates 55---------------------- 56 57Handlebars allows templates to be precompiled and included as javascript code rather than the handlebars template allowing for faster startup time. Full details are located [here](https://handlebarsjs.com/installation/precompilation.html). 58 59Differences Between Handlebars.js and Mustache 60---------------------------------------------- 61Handlebars.js adds a couple of additional features to make writing 62templates easier and also changes a tiny detail of how partials work. 63 64- [Nested Paths](https://handlebarsjs.com/guide/expressions.html#path-expressions) 65- [Helpers](https://handlebarsjs.com/guide/expressions.html#helpers) 66- [Block Expressions](https://handlebarsjs.com/guide/block-helpers.html#basic-blocks) 67- [Literal Values](https://handlebarsjs.com/guide/expressions.html#literal-segments) 68- [Delimited Comments](https://handlebarsjs.com/guide/#template-comments) 69 70Block expressions have the same syntax as mustache sections but should not be confused with one another. Sections are akin to an implicit `each` or `with` statement depending on the input data and helpers are explicit pieces of code that are free to implement whatever behavior they like. The [mustache spec](https://mustache.github.io/mustache.5.html) defines the exact behavior of sections. In the case of name conflicts, helpers are given priority. 71 72### Compatibility 73 74There are a few Mustache behaviors that Handlebars does not implement. 75- Handlebars deviates from Mustache slightly in that it does not perform recursive lookup by default. The compile time `compat` flag 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. 76- The optional Mustache-style lambdas are not supported. Instead Handlebars provides its own lambda resolution that follows the behaviors of helpers. 77- Alternative delimiters are not supported. 78 79 80Supported Environments 81---------------------- 82 83Handlebars has been designed to work in any ECMAScript 3 environment. This includes 84 85- Node.js 86- Chrome 87- Firefox 88- Safari 5+ 89- Opera 11+ 90- IE 6+ 91 92Older versions and other runtimes are likely to work but have not been formally 93tested. The compiler requires `JSON.stringify` to be implemented natively or via a polyfill. If using the precompiler this is not necessary. 94 95Performance 96----------- 97 98In a rough performance test, precompiled Handlebars.js templates (in 99the original version of Handlebars.js) rendered in about half the 100time of Mustache templates. It would be a shame if it were any other 101way, since they were precompiled, but the difference in architecture 102does have some big performance advantages. Justin Marney, a.k.a. 103[gotascii](http://github.com/gotascii), confirmed that with an 104[independent test](http://sorescode.com/2010/09/12/benchmarks.html). The 105rewritten Handlebars (current version) is faster than the old version, 106with many performance tests being 5 to 7 times faster than the Mustache equivalent. 107 108 109Upgrading 110--------- 111 112See [release-notes.md](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md) for upgrade notes. 113 114Known Issues 115------------ 116 117See [FAQ.md](https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md) for known issues and common pitfalls. 118 119 120Handlebars in the Wild 121---------------------- 122 123* [Assemble](http://assemble.io), by [@jonschlinkert](https://github.com/jonschlinkert) 124 and [@doowb](https://github.com/doowb), is a static site generator that uses Handlebars.js 125 as its template engine. 126* [Cory](https://github.com/leo/cory), by [@leo](https://github.com/leo), is another tiny static site generator 127* [CoSchedule](http://coschedule.com) An editorial calendar for WordPress that uses Handlebars.js 128* [dashbars](https://github.com/pismute/dashbars) A modern helper library for Handlebars.js. 129* [Ember.js](http://www.emberjs.com) makes Handlebars.js the primary way to 130 structure your views, also with automatic data binding support. 131* [Ghost](https://ghost.org/) Just a blogging platform. 132* [handlebars_assets](http://github.com/leshill/handlebars_assets): A Rails Asset Pipeline gem 133 from Les Hill (@leshill). 134* [handlebars-helpers](https://github.com/assemble/handlebars-helpers) is an extensive library 135 with 100+ handlebars helpers. 136* [handlebars-layouts](https://github.com/shannonmoeller/handlebars-layouts) is a set of helpers which implement extendible and embeddable layout blocks as seen in other popular templating languages. 137* [hbs](http://github.com/donpark/hbs): An Express.js view engine adapter for Handlebars.js, 138 from Don Park. 139* [koa-hbs](https://github.com/jwilm/koa-hbs): [koa](https://github.com/koajs/koa) generator based 140 renderer for Handlebars.js. 141* [jblotus](http://github.com/jblotus) created [http://tryhandlebarsjs.com](http://tryhandlebarsjs.com) 142 for anyone who would like to try out Handlebars.js in their browser. 143* [jQuery plugin](http://71104.github.io/jquery-handlebars/): allows you to use 144 Handlebars.js with [jQuery](http://jquery.com/). 145* [Lumbar](http://walmartlabs.github.io/lumbar) provides easy module-based template management for 146 handlebars projects. 147* [Marionette.Handlebars](https://github.com/hashchange/marionette.handlebars) adds support for Handlebars and Mustache templates to Marionette. 148* [sammy.js](http://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey, 149 supports Handlebars.js as one of its template plugins. 150* [SproutCore](http://www.sproutcore.com) uses Handlebars.js as its main 151 templating engine, extending it with automatic data binding support. 152* [YUI](http://yuilibrary.com/yui/docs/handlebars/) implements a port of handlebars 153* [Swag](https://github.com/elving/swag) by [@elving](https://github.com/elving) is a growing collection of helpers for handlebars.js. Give your handlebars.js templates some swag son! 154* [DOMBars](https://github.com/blakeembrey/dombars) is a DOM-based templating engine built on the Handlebars parser and runtime **DEPRECATED** 155* [promised-handlebars](https://github.com/nknapp/promised-handlebars) is a wrapper for Handlebars that allows helpers to return Promises. 156* [just-handlebars-helpers](https://github.com/leapfrogtechnology/just-handlebars-helpers) A fully tested lightweight package with common Handlebars helpers. 157 158External Resources 159------------------ 160 161* [Gist about Synchronous and asynchronous loading of external handlebars templates](https://gist.github.com/2287070) 162 163Have a project using Handlebars? Send us a [pull request][pull-request]! 164 165License 166------- 167Handlebars.js is released under the MIT license. 168 169[pull-request]: https://github.com/handlebars-lang/handlebars.js/pull/new/master 170