Backbone Template-Manager
Simple template manager for your Backbone application. This library allows you to:
- Use a template manager to query templates from the DOM or from a server.
- Create view with a template manager to download templates.
- Render view with your model and a default compile function (use
_.template
by default).
Installation
- With
npm
:npm install backbone-template-manager --save
. - With
bower
:bower install backbone-template-manager --save
.
Once installed, import the library using ES6 import, or import the ES5 script (transpiled with babel):
View
Here is an example:
;; { thismodel = id: 1 name: 'John Doe' ; } { return 'my-template'; }
What happens here?
- View extends from
TemplateView
class. - A model is attached to the view in the
initialize
function (class Backbone application). - A
template
function return the template id associated to the view.
And that's all!
Note that following events will be triggered:
render:loading
when the view start rendering (before any template download and rendering).render:success
when the view is fully rendered (templates are fetched, view is up to date).render:error
when the view cannot be rendered because of missing templates.
How it works?
The render
method comes with a default implementation:
- Get the templates to fetch.
- Without templates, the
render
method do nothing). - Otherwise:
- Trigger
render:loading
event and execute the viewonBeforeRender
method. - Get the default template manager of the view (basically, calls
templateManager
method view). - Download the template using its id (what happens here precisely depends on the template manager, more details later).
- Calls the
toJSON
method from the view: by default return an object containing amodel
property (withmodel.toJSON
as the value) and/or acollection
property (result ofcollection.toJSON
method). - Render the view using default compile function (use
_.template
under the hood). - Trigger
render:success
event and execute viewonRendered
andonRender
methods without errors. - Trigger
render:error
event and execute viewonRendered
andonRenderError
if something bad happened.
- Trigger
How templates are downloaded?
By default, templates are downloaded using Backbone.ajax
.
- The default http method is
GET
. - The requested URL is built with:
- The template manager prefix (default is
/templates/
). - The template id.
- The template manager suffix (default is
.template.html
).
- The template manager prefix (default is
In the example below, the requested URL will be /templates/foo.template.html
.
Note that all downloaded templates are put in a cache, so don't worry about network request when you render a view several times!
Override default template manager
By default, the template manager fetch templates from a remote server using Backbone.ajax
.
Let's say you want to define your DOM in your HTML, such as:
You can override the default template with an instance of the DomTemplateManager
:
;; // Just given an instance of the new template manager as the first parameter.// Should be done when your application starts.;
By default, the selector in the DOM will be defined as [data-template-id="${templateId}"]
, but it may be override (see below).
Now, the template
method of your view must return the selector in the DOM to query the DOM appropriately:
;; { thismodel = id: 1 name: 'John Doe' ; } { return 'my-template'; }
Note that the overrideTemplateManager
method can also be used to override the default prefix
, suffix
and method
parameters:
;; // Just given an instance of the new template manager as the first parameter.// Should be done when your application starts.;
Override default compile function
The default compile function can be overridden with a custom implementation. Let's say you want to use Mustache (or Handlebars) as the default template engine:
;; // Override the compile function.// Compile function must return a function that can be used to render template// with data model.// Should be done when your application starts.;
Dealing with partials
Some libraries, such as Mustache, allow you to define partials:
Hello {{ name }} {{ > user-view }}
In the template below, user-view
is a partial that can be set during template compilation:
Mustache;
This little library can deal with partials, simply defined an array of templates in your view:
;; { thismodel = id: 1 name: 'John Doe' ; } { return 'my-template' 'user-view'; }
When an array is defined as a template view, the first entry in the array will be the first
argument of the compile
function, other templates will be given as second argument as a dictionary where the
entry is the template id and the value is the template. You can now render templates easily:
;; ;
Dealing with JST
The remote template manager can be optimized by sending all templates in a javascript file stored in a variable (see here) and query this dictionary instead of querying the remote server:
;; // Just given an instance of the new template manager as the first parameter.// Should be done when your application starts.;
Three options are available:
JST: true
: assume that templates may already exist inwindow.JST
.JST: '__JST__'
: assume that templates may already exist inwindow.__JST__
.JST: {}
: send the templates object directly during the construction.
If templates does not exist in the JST variable, a classic HTTP request will be made as a fallback. Note that templates should be stored with the template id as the key (not the full URL), for example:
;; { thismodel = id: 1 name: 'John Doe' ; } { return 'my-template'; }
The template manager will assume that window.JST
will be equal to:
History
This little library has been created in 2013 (still used in production) and open sourced in 2016 after a rewrite in ES6.
License
MIT.