Skip to content

Instantly share code, notes, and snippets.

@alunny
Created February 24, 2011 22:40
Show Gist options
  • Save alunny/843056 to your computer and use it in GitHub Desktop.
Save alunny/843056 to your computer and use it in GitHub Desktop.
// lazy loading and caching templates
// callback receives rendered html - can write to the DOM or whatnot
var TMPL_PREFIX = 'tmpl.'
function renderTemplate(callback, identifier, data) {
// check localStorage for identifier
var data = data || {},
key = TMPL_PREFIX + identifier,
url = 'templates/' + identifier + '.mustache',
tmpl;
if (tmpl = window.localStorage.getItem(key)) {
callback(Mustache.to_html(tmpl, data));
} else {
x$().xhr(url, function () {
var tmpl = this.responseText;
window.localStorage.setItem(key, tmpl);
callback(Mustache.to_html(tmpl, data));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment