Skip to content

wtfil/jt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Super easy template engine. Write templates in json!

var jt = require('jt');

var template = jt({
    tag: 'div',
    content: 'foo'
});

console.log(template()); // <div>foo</div>

Install

npm install jt

You can use jt as CommonJS or AMD module.

API

jt returns function (template) that can be callen in future.

You can create template from string, array, json and functions:

var template = jt('hello');

template(); // 'hello'
var template = jt(['hello ', 'world']);

template(); // 'hello world'
var template = jt({
    tag: 'snap',
    class: 'text'
    content: ['hello ', 'world']
});

template(); // '<span class="text">hello world</span>'

Use function as argument to create dynamic templates

var template = jt(function (who) {
    return {
        tag: 'div',
        content: ['hello ', who]
    }

});

template('world'); // '<span class="text">hello world</span>'
var template = jt(function (items) {
	return {
		tag: 'ul',
		class: 'list',
		content: items.map(function (item) {
			return {
				tag: 'li',
				class: 'item',
				content: {
					tag: 'a',
					href: item.url,
					content: item.text
				}
			};
		})
	};
});

var items = [
    {url: '/foo', text: 'get foo'},
    {url: '/bar', text: 'get bar'}
];

template(items)

result:

<ul class="list">
	<li class="item"><a href="/foo">get foo</a></li>
	<li class="item"><a href="/bar">get bar</a></li>
</ul>
var template = jt(function (who, action) {
    return {
        tag: 'div',
        content: [action, ' ', who]
    }

});

template('world', 'hello'); // '<span class="text">hello world</span>'

If You dont want to create template for one call, You just can get template as string (not function):

js.compile({
    tag: 'img',
    src: '/some.png'
}); // '<img src="/some.png"/>'

About

json templates

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published