Skip to content

Instantly share code, notes, and snippets.

@aldwyish
Last active December 19, 2015 05:49
Show Gist options
  • Select an option

  • Save aldwyish/5906937 to your computer and use it in GitHub Desktop.

Select an option

Save aldwyish/5906937 to your computer and use it in GitHub Desktop.
/**
Usage: Just include this script after Marionette and Handlebars loading
IF you use require.js add script to shim and describe it in the requirements
*/
(function(Handlebars, Marionette) {
Marionette.Handlebars = {
path: 'templates/',
extension: '.handlebars'
};
Marionette.TemplateCache.prototype.loadTemplate = function(templateId) {
var template, templateUrl;
if (Handlebars.templates && Handlebars.templates[templateId]) {
return Handlebars.templates[templateId];
}
try{
template = Marionette.$(templateId).html();
} catch(e) {}
if (!template || template.length === 0) {
templateUrl = Marionette.Handlebars.path + templateId + Marionette.Handlebars.extension;
Marionette.$.ajax({
url: templateUrl,
success: function(data) {
template = data;
},
async: false
});
if (!template || template.length === 0){
throw "NoTemplateError - Could not find template: '" + templateUrl + "'";
}
}
return template;
};
Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {
if (typeof rawTemplate === "function"){
return rawTemplate;
}
else {
return Handlebars.compile(rawTemplate);
}
};
}(Handlebars, Marionette));
@jblanche
Copy link

jblanche commented Jul 2, 2013

Perfect, I did it another way (changing the load proto to pass the templateId to the compileTemplate function) but this way is cleaner.
Thanks :)

@aldwyish
Copy link
Author

aldwyish commented Jul 2, 2013

your welcome, never thought someone will find it useful, at least not this fast! You probably will do it this way if you peaked at Marionette.Renderer annotated code

@aldwyish
Copy link
Author

aldwyish commented Jul 3, 2013

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