Created
February 24, 2015 09:08
-
-
Save jajeffries/91a8db821ac7ce7a47b3 to your computer and use it in GitHub Desktop.
Simple javascript 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
// 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