tljs
v0.2.0
Published
[![Travis](https://img.shields.io/travis/theJian/tljs.svg?style=flat-square)](https://travis-ci.org/theJian/tljs.svg?branch=master)
Downloads
4
Readme
tljs
Tiny template engine powered by es6 template literals
- Super small (< 30 lines).
- Pure JavaScript syntax.
- Template includes
Install
npm install tljs
Usage
import tl from 'tljs';
// template
const t = tl`
<div>
${' if (username) { '}
<p>${' echo(username) '}</p>
${' } '}
</div>`;
// render
t({ username: 'theJian' });
// output
<div>
<p>theJian</p>
</div>
/////////////////////////// Generate JSON ////////////////////////////////
const jsonTl = tl`
{
username: ${' echo(username) '}
}`;
jsonTl({ username: 'theJian' });
API
Echo
Instead of defining some fancy syntax for inserting variables to the output, echo
, a good old JavaScript function, has provided to handle this job.
`Here is the ${' echo(username) '}`
Include
To insert contents from another file.
<div>
${' include("./header.js", { title: title }) '}
${' posts.forEach(post => { '}
<p>
${ 'include("./post.js", { post: post }) '}
</p>
${' } '}
</div>
To use this feature you have to pass a require
function when rendering. See Example.
The first argument is the path of included file, the second is the values passed to it.
License
MIT@theJian