Skip to content

Instantly share code, notes, and snippets.

@erming
Last active August 29, 2015 14:02
Show Gist options
  • Save erming/04262f779517515e9343 to your computer and use it in GitHub Desktop.
Save erming/04262f779517515e9343 to your computer and use it in GitHub Desktop.
Render helper with template caching.
// Requires:
// <script src="mustache.js"></script>
// Template cache
var tpl = [];
//
// Render templates.
//
// Usage:
// render("#tpl", {foo: "bar"})
//
// Return String
//
function render(id, data) {
tpl[id] = tpl[id] || $(id).html();
if (!data) {
// If no data is supplied, return template instead.
return tpl[id];
}
return Mustache.Render(
tpl[id],
data
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment