Created
April 19, 2016 06:30
-
-
Save ojii/85bee03759019343e1c712144180284c to your computer and use it in GitHub Desktop.
amazing javascript template engine.
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
'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