Last active
August 29, 2015 14:02
-
-
Save erming/04262f779517515e9343 to your computer and use it in GitHub Desktop.
Render helper with template caching.
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
// 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