Let's close the ultra-small library cycle with some awesome array-based templating. 323 bytes minified.
Just download the minified version here or include it into your code:
<script src="//cdn.rawgit.com/plugnburn/07c383da5f151a54d0b2/raw/896334671e40c42296b10949383b92db8bdb8fdf/xt.min.js"></script>
XT.js consists of just one function: XT(domStructure)
. The main feature of this function is that it returns a ready DocumentFragment instance ready to be inserted anywhere. The domStructure
toplevel array must contain arrays only, and any internal array syntax is pretty straightforward:
- the first array element is the tag name;
- any scalar value (string or number) denotes element text content;
- any object denotes element attributes in name-value form;
- any array denotes the nested element of the same structure.
That's it.
The following code:
XT([
['div', {'data-attr1': 23},
['a', {href: 'http://example.com'}, 'Example text',
['span', ' (additional span)']
]
]
])
will produce a DocumentFragment with the following HTML representation:
<div data-attr1="23">
<a href="http://example.com">Example text<span> (additional span)</span></a>
</div>
Update: for dynamic CSS rules construction, see XS.js.