Created
May 15, 2013 12:41
-
-
Save ThiefMaster/5583721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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