Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created December 12, 2012 05:09
Show Gist options
  • Save tbranyen/4265026 to your computer and use it in GitHub Desktop.
Save tbranyen/4265026 to your computer and use it in GitHub Desktop.
Very basic Ember.Handlebars compiling with RequireJS and the text plugin.
/* RequireJS Ember.Handlebars Plugin v0.1.0
* Copyright 2012, Tim Branyen (@tbranyen)
* hbs.js may be freely distributed under the MIT license.
*/
define(["ember", "text"], function(Ember) {
var hbs = {
version: "0.1.0",
// Invoked by the AMD builder, passed the path to resolve, the require
// function, done callback, and the configuration options.
load: function(name, req, load, config) {
// Dojo provides access to the config object through the req function.
if (!config) {
config = require.rawConfig;
}
var tpl = config.hbs;
// Get the text for the template and compile it for Ember.
req(["text!" + tpl.prefix + name + "." + tpl.ext], function(contents) {
// Use global for now (maybe this?)
return load(Ember.Handlebars.compile(contents));
});
}
};
return hbs;
});
@Pencroff
Copy link

Hi. Thanks for your simple solution.
I little improved it.

/* RequireJS Ember.Handlebars Plugin v0.1.1
 * Copyright 2012, Tim Branyen (@tbranyen)
 * Copyright 2013, Sergii Danilov (@Pencroff)
 * hbs.js may be freely distributed under the MIT license.
 */
define(["ember", "text"], function (Ember) {

    var hbs = {
        version: "0.1.1",
        // Invoked by the AMD builder, passed the path to resolve, the require
        // function, done callback, and the configuration options.
        load: function (name, req, load, config) {
            "use strict";
            var tpl;
            // Dojo provides access to the config object through the req function.
            if (!config) {
                config = require.rawConfig;
            }
            tpl = config.hbs;
            if (!tpl) {
                tpl = {}
            }
            if (!tpl.prefixName) {
                tpl.prefixName = '';
            }
            if (!tpl.templateExtension) {
                tpl.templateExtension = 'hbs';
            }
            // Get the text for the template and compile it for Ember.
            req(["text!" + tpl.prefixName + name + "." + tpl.templateExtension], function (contents) {
                // Use global for now (maybe this?)
                return load(Ember.Handlebars.compile(contents));
            });
        }
    };
    return hbs;
});

@millermedeiros
Copy link

would be good to precompile the templates during build, see: jfparadis/requirejs-handlebars#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment