Skip to content

Instantly share code, notes, and snippets.

@fabslab
Created August 22, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabslab/6312881 to your computer and use it in GitHub Desktop.
Save fabslab/6312881 to your computer and use it in GitHub Desktop.
RequireJS loader plugin for returning a compiled template. Underscore is used here - swap out for your favourite template library.
// RequireJS loader plugin for returning a compiled template
// Underscore is used here, swap out for your favourite template library
define(['text', 'underscore'], function(textLoader, _) {
var buildMap = {};
return {
load: function(name, req, onload, config) {
onTextLoad = function(content) {
var compiledTemplate = _.template(content);
if (config.isBuild) {
buildMap[name] = compiledTemplate;
}
onload(compiledTemplate);
}
textLoader.load(name, req, onTextLoad, config);
},
// for the optimiser:
write: function(pluginName, moduleName, write) {
if (buildMap.hasOwnProperty(moduleName)) {
var content = textLoader.jsEscape(buildMap[moduleName]);
write.asModule(pluginName + "!" + moduleName, "define(function() { return " + content + "; });\n");
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment