A tiny js template engine, < 1KB, just enough.
<div>{{= value }}</div>
<div>{{= obj.key }}</div>
<div>{{= obj["key"] }}</div>
<div>{{= bool ? "true" : "false" }}</div>
<div>{{= value || "default" }}</div>
<div>{{= numb + 1 }}</div>" when you want a string literal.
<div>{{@ value }}</div>{{ if truthy }}<div>will output</div>{{ /if }}
{{ if falsy }}
<div>will not output</div>
{{ else }}
<div>will output</div>
{{ /if }}
{{ if falsy }}
<div>will not output</div>
{{ elseif truthy }}
<div>will output</div>
{{ else }}
<div>will not output</div>
{{ /if }}<ul>
{{ each array }}
<li>{{= $key}}: {{= $value}}</li>
{{ /each }}
</ul>
<ul>
{{ each object }}
<li>{{= $key}}: {{= $value}}</li>
{{ /each }}
</ul>Support both array and object type of data.
$keyis index inarrayand key inobject;$valueis item inarrayand value inobject;
Compile template and return a render function.
Parameters:
{string}tpltemplate source
Returns:
{function}renderrender function.
Example
const helloTpl = '<h1>Hello {{= name}}</h1>';
const helloRender = compile(helloTpl);
document.querySelector('#user1').innerHTML = helloRender({name: 'John'});
document.querySelector('#user2').innerHTML = helloRender({name: 'Duke'});Compile template and return render result.
Parameters:
{string}tpltemplate source{object}datatemplate model
Returns:
{string}htmlrender result.
Example
const helloTpl = '<h1>Hello {{= name}}</h1>';
document.querySelector('#user1').innerHTML = render(helloTpl, {name: 'John'});
document.querySelector('#user2').innerHTML = render(helloTpl, {name: 'Duke'});Actually if you need render a template multiple times, please use compile() to cache the render function.