Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precompiled Handlebars templates in Backbone with Requirejs?

I've been messing around with a backbone.js app using require.js and a handlebars templates (I've added the AMD module stuff to handlebars) and just read that pre-compiling the templates can speed it up a fair bit.

I was wondering how I would go about including the precompiled templates with requirejs. I have a fair few templates to compile (upwards of 15), so i'm not sure if they should all be in the same output file or have their own once compiled. Also, from what it seems, the compiled templates share the same Handlebars namespace that the renderer script uses, so I'm not sure how I would go about that when requiring the templates in my files.

Any advice would be awesome!

like image 814
Tom Brunoli Avatar asked Mar 27 '12 10:03

Tom Brunoli


People also ask

How do I use precompiled handlebars templates?

First you need to install handlebars with node by running this command. If you don't have node, go ahead and install it first. It's really quick and painless. Then with all of your templates in that folder, open up the command prompt, navigate to the folder right above the js folder.

What is handlebars template engine?

Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions. A handlebars expression is a {{ , some contents, followed by a }} .


1 Answers

A simple approach is to create a RequireJS plugin based on the existing text! plugin. This will load and compile the template. RequireJs will cache and reuse the compiled template.

the plugin code:

// hbtemplate.js plugin for requirejs / text.js
// it loads and compiles Handlebars templates
define(['handlebars'],
function (Handlebars) {

    var loadResource = function (resourceName, parentRequire, callback, config) {
        parentRequire([("text!" + resourceName)],
            function (templateContent) {
                var template = Handlebars.compile(templateContent);
                callback(template);
            }
        );
    };

    return {
        load: loadResource
    };

});

configuration in main.js:

require.config({
    paths: {
        handlebars: 'libs/handlebars/handlebars',
        hb: 'libs/require/hbtemplate',
    }
});

usage in a backbone.marionette view:

define(['backbone', 'marionette',
        'hb!templates/bronnen/bronnen.filter.html',
        'hb!templates/bronnen/bronnen.layout.html'],
        function (Backbone, Marionette, FilterTemplate, LayoutTemplate) {
        ...

In case you use the great Backbone.Marionette framework you can override the default renderer so that it will bypass the builtin template loader (for loading/compiling/caching):

Marionette.Renderer = {
    render: function (template, data) {
        return template(data);
    }
};
like image 108
Boris Van Woerkom Avatar answered Sep 21 '22 09:09

Boris Van Woerkom