Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created May 15, 2013 12:41
Show Gist options
  • Save ThiefMaster/5583721 to your computer and use it in GitHub Desktop.
Save ThiefMaster/5583721 to your computer and use it in GitHub Desktop.
// Templating
(function($, global) {
'use strict';
var templates = null;
function loadTemplates() {
if(!$.isReady) {
console.error('Tried loading templates before DOMReady');
console.trace();
return false;
}
templates = {};
$('script.template').each(function() {
var $this = $(this);
templates[$this.data('id')] = Handlebars.compile($this.html().trim());
}).remove();
return true;
}
global.renderTemplate = function renderTemplate(name, context) {
if(templates === null && !loadTemplates()) {
return 'Cannot use templates before DOMReady. ';
}
var tplFunc = templates[name] || function() {
console.error('Template not found: ' + name);
console.trace();
return 'Template "' + name + '" not found. ';
};
return $($.parseHTML(tplFunc(context || {})));
};
})(jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment