Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Minimal templating on steroids.

License

NotificationsYou must be signed in to change notification settings

handlebars-lang/handlebars.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Travis Build StatusAppveyor Build StatusSelenium Test Status

Handlebars.js

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/.

Installing

See ourinstallation documentation.

Usage

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.

Precompiling Templates

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.

Differences Between Handlebars.js and Mustache

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.

Compatibility

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 timecompat 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.
  • 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.

Supported Environments

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.

Selenium Test Status

Performance

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.

Upgrading

Seerelease-notes.md for upgrade notes.

Known Issues

SeeFAQ.md for known issues and common pitfalls.

Handlebars in the Wild

  • 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.

External Resources

Have a project using Handlebars? Send us apull request!

License

Handlebars.js is released under the MIT license.


[8]ページ先頭

©2009-2025 Movatter.jp