Cached Templates
:)
by Chase Wilson
HTML
<script type="text/template" id="something">
<h1>Heyo</h1>
</script>
<script type="text/template" id="other-thing">
<h1>Heyo</h1>
</script>
JavaScript
Pirate = (function (undef) {
var templates = {}
var Pirate = {
config: {
template: {
tag: 'script'
,identifier: 'text/template'
}
,check: 5000
}
,getTemplate: function (key) {
return templates[key] || (function () {
for (var k in Pirate.__scripts) {
if(Pirate.__scripts.hasOwnProperty(k)) {
var script = Pirate.__scripts[k]
var type = script.type
var id = script.id
if( script.type === Pirate.config.template.identifier
&& (templates[id] === undef) ) {
templates[id] = {node: script, text: script.innerHTML}
if (id === key) return templates[id]
}
}
}
return undef
}())
setTimeout(Pirate.getTemplate,Pirate.config.check)
}
};
Pirate.__scripts = document.getElementsByTagName(Pirate.config.template.tag)
Pirate.getTemplate()
return Pirate
}());
window.addEvent('domready', function () {
setTimeout(function () {
var script = document.createElement('script')
script.type = 'text/template'
script.innerHTML = '<h1>For Yayo</h1>'
script.id = 'yayo'
document.body.appendChild(script)
console.log(Pirate)
}, 1000)
})