Created
February 15, 2012 18:43
-
-
Save sebmarkbage/1838080 to your computer and use it in GitHub Desktop.
HTML Template
This file contains hidden or 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
var template = function(html){ | |
var root = document.createElement('root'); | |
root.innerHTML = html; | |
var nodes = root.getElementsByTagName('*'), ids = {}; | |
for (var i = 0, l = nodes.length; i < l; i++){ | |
var id = nodes[i].getAttribute('id'); | |
if (id){ | |
nodes[i].removeAttribute('id'); | |
ids[id] = i; | |
} | |
} | |
return function(){ | |
var nodes = root.cloneNode(true).getElementsByTagName('*'); | |
return function(id){ | |
return nodes[ids[id]]; | |
} | |
}; | |
}; | |
var create = template('<div id="root"><h1 id="headline"></h1><h2 id="subheadline"></h2></div>'); | |
var instance = create(); | |
instance('headline').textContent = 'test'; | |
document.body.appendChild(instance('root')); | |
var instance = create(); | |
instance('headline').textContent = 'test2'; | |
document.body.appendChild(instance('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment