Skip to content

Instantly share code, notes, and snippets.

@jajeffries
Created February 24, 2015 09:08
Show Gist options
  • Save jajeffries/91a8db821ac7ce7a47b3 to your computer and use it in GitHub Desktop.
Save jajeffries/91a8db821ac7ce7a47b3 to your computer and use it in GitHub Desktop.
Simple javascript template caching
// Simply call getTemplate with the name of your template. Templates should be stored in the DOM using:
// <script type='text/html' id='tmpl-YOU_TEMPLATE_NAME'>...</script>
(function () {
var _templates = {};
function loadTemplate(templateName) {
var templateId = "#tmpl-" + templateName,
$template = $(templateId);
_templates[templateName] = $template.html();
$template.remove();
}
function getTemplate(templateName) {
if(!_templates[templateName]) {
loadTemplate(templateName);
}
return _templates[templateName];
}
// ...
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment