Skip to content

Instantly share code, notes, and snippets.

@ojii
Created April 19, 2016 06:30
Show Gist options
  • Save ojii/85bee03759019343e1c712144180284c to your computer and use it in GitHub Desktop.
Save ojii/85bee03759019343e1c712144180284c to your computer and use it in GitHub Desktop.
amazing javascript template engine.
'use strict';
/*
A minimal "template" system. Just some wrappers around createTextNode and
createElement.
Usage:
E('div', {'class': 'foo'}, [
E('span', {'style': 'color:red;'}, [T('Error')])
]);
Results in:
<div class='foo'>
<span style='color: red;'>Error</span>
</div>
*/
export function T(text){
return document.createTextNode(text);
}
export function E(name, attrs, children){
const ele = document.createElement(name);
for (var key in attrs){
if (attrs.hasOwnProperty(key)){
ele.setAttribute(key, attrs[key]);
}
}
if (children){
children.map((child) => {ele.appendChild(child);});
}
return ele;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment