Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
Created February 15, 2012 18:43
Show Gist options
  • Save sebmarkbage/1838080 to your computer and use it in GitHub Desktop.
Save sebmarkbage/1838080 to your computer and use it in GitHub Desktop.
HTML Template
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