Backbone.Handlebars provides a solid integration of Handlebars into Backbone with:
{{user.name}}
compiles to user.get('name')
each
helper is able to iterate through
a Backbone.Collection.<h1>User details</h1>{{view UserView model=user}}
You can refer to the project's website for a nice HTML documentation.
The raw sources can be navigated on GitHub.
The distributed sources can be found in the dist/
directory or
downloaded directly via one of the following links:
You can also use Bower to install backbone.handlebars:
$ bower install backbone.handlebars --save
Backbone.Handlebars extends Handlebars to support fetching Backbone.Model
attributes through model.get(attribute)
:
var user = new Backbone.Model({name: 'World'});
var fn = Handlebars.compile('Hello {{name}}!');
fn(user);
// Hello World!
Notice that we directly pass the user as context and not user.toJSON()
.
Instead of using the dot notation user.name
, it will detect that we are
managing a Backbone.Model and use user.get('name')
.
Backbone.Handlebars override the default each
helper to support looping
through a Backbone.Collection:
var numbers = new Backbone.Collection(['one', 'two', 'three', 'four']);
var fn = Handlebars.compile('<ul>{{#each numbers}}<li>{{number}}</li>{{/each}}</ul>');
fn({numbers: numbers});
// <ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>
var User = Backbone.Model.extend();
var AppView = Backbone.HandlebarsView.extend({
template: '<h1>Hello</h1>{{view "HelloView" model=this}}'
});
var HelloView = Backbone.HandlebarsView.extend({
template: 'Hello {{name}}'
});
var app = new AppView({model: new User({name: 'Loïc'})});
app.render().$el.appendTo('#app');
Backbone.Handlebars is tested, you can see its test suite running online. Also, Travis CI takes care of continuously running this test suite: .
{{view "My.Deep.View"}}
.Copyright (c) 2013 Loïc Frering, licensed under the MIT license. See the LICENSE file for more informations.