This extends the ultra-small, array-based templating library XT.js with functional manipulation abilities. This extension adds to xtf.js
6 lines of code and to xtf.min.js
155 minified bytes.
Download the minified version here or include it into your code:
<script src="https://cdn.rawgit.com/METACEO/df988bf134e3fdb18e8cafe9f6e4b7de/raw/fc1a558989f3b7f142d02bf7b8b008054b82d5e5/xtf.min.js"></script>
Follow the XT.js syntax instructions with the following addition:
- any array, with the first element an object, denotes functions to apply upon the element.
Provided functions will be called with two arguments, the parent element and the parent object. The parent element can be manipulated before it's appended and the parent object can store data for the manipulations. The object of will call upon locally available functions, or - if provided a string - will lookup a client-provided function with the string as the key. Your server can provide array-based templates and your clients can locally process conditions and utilize dyanamic variables.
The following code:
var funcs = {}
funcs.myFunction = function($a, data) {
$a.appendChild(XTF([["i", data.thisWillBeItalicized]]))
}
XTF([
['div', {'data-attr1': 23},
['a', {href: 'http://example.com'}, 'Example text ',
[{viaString: 'myFunction'},{thisWillBeItalicized: 123}],
['span', ' (additional span)']
]
]
],funcs)
will produce a DocumentFragment with the following HTML representation:
<div data-attr1="23">
<a href="http://example.com">Example text <i>123</i><span> (additional span)</span></a>
</div>
The following code:
function myFunction($a, data) {
$a.appendChild(XTF([["i", data.thisWill.be.italicized]]))
}
XTF([
['div', {'data-attr1': 23},
['a', {href: 'http://example.com'}, 'Example text ',
[{viaLocal: myFunction},{thisWill: {be: {italicized: 123}}}],
['span', ' (additional span)']
]
]
])
will produce a DocumentFragment with the following HTML representation:
<div data-attr1="23">
<a href="http://example.com">Example text <i>123</i><span> (additional span)</span></a>
</div>