Jinsoku - template engine
High performance Javascript template engine inspired by Jade's power and doT's speed.
Contents
Features
- Template inheritance
- Static includes
- Asynchronous partials loading support
- Blocks extending and cloning
- Iterators and Conditionals
- Extensible ...
Installation
via npm:
npm install jinsoku
Dependencies
Jinsoku requires only 2 libs: For Node.js:
- Async
- Cheerio
For browser:
- Async
- jQuery
Browser support
Templates are loaded with jQuery.ajax
method by default.
Same as on Node.js we can replace Jinsoku.resolve
and Jinsoku.template
methods with our custom. This make Jinsoku pretty easy to integrate into any web application or framework.
Please note that by default jQuery will execute any <script>
that will be found in your templates content.
Public API
resolve
resolve path to template filestemplate
read templates contentcompile
compile template into functionrender
compile and run template
var Path = ;var Fs = ; var Jinsoku = ; var root = process + Pathsep; // resolve paths to template filesJinsoku { return Path + '.html';} // get template content from file system, cache or anythig elseJinsoku { var self = this; path = self; if selfcachepath ; Fs;} var path = 'home'; // this template path after resolving will be: /path/to/app/views/home.html // compile template to functionJinsoku; // render template// this will compile template and generated functionJinsoku;
Options
path
:root +'views'+ Path.sep
Default root path for templatesdataname
:data
Locals variable object nameextract
:true
Extract data to local scope. If false, locals will be available asdataname.varname
.cache
:true
Cache files content.extension
:.html
Default template file extension.
To set options we can call:
Jinsoku;// orJinsoku;// or set them as data.options when call rendervar data = ... options: cache: false Jinsoku;// as second argument for compileJinsoku;
Syntax
Jinsoku allow different ways to define template.
- As attributes
// include partial into tag // each iterator over array #[user.username]
- As
<j>
tag
// extend some template // define a block
- With square brackets
// simple include[include: auth/login] // array iterator[each:users :user:i] #[i+1]. #[user.username][/each]
Template inheritance
Jinsoku supports template inheritance via extend
keyword.
Suppose we have the following template content.html:
Page title Page content
Now to extend this template in home.html:
© 2012 MyCompany
and result will be:
Page title Page content © 2013 MyCompany
Includes
Includes allow you to statically include parts of content.
For example home.html which has head and body in separate files:
// or here [include: body]
Blocks
Each template could be splitted in more parts named blocks.Then after extending this template we may replace content of the blocks, prepend and append content, or even clone them.
Suppose we have this head.html template:
[block: meta] [/block]
now in home.html to extend and add something to our head:
Body content
this will add meta tag to meta block and app.js script to scripts block so final home.html will look like so:
Body content
To replace a block we need just to redefine it:
// replace scripts block [block: scripts] [/block]// now scripts block will contain only main.js script
Array and Object iterators
Jinsoku also supports friendly iterators over arrays(each) and objects(for).
For arrays: var items = ['one', 'two', 'three', 'four'];
#[i]. #[item] // or #[i]. #[item] // or[each:items :item:i] #[i]. #[item][/each]
i
is current index, optional
For objects: var obj = { foo: 'bar' };
#[key]: #[value] // or #[key]: #[value] // or[for:obj :value:key] #[key]: #[value][/for]
Conditionals
Jinsoku has shortcut support for if
and switch
statements.
[if: User.logged_in] // if (User.logged_in) { Welcome back, #[User.username]![: User.banned] // else if (User.banned) { Access denied, you are banned![:] // else { Hello guest, please login![/if] // } [case: User.role] // switch(User.role) { [:'administrator'] // case 'administrator': #[User.username] is administrator. [:'moderator'] // break; case 'moderator': #[User.username] is moderator. [:] // break; default: #[User.username] is user.[/case] // break; }
Variables
// simple#[page.title] // escape html![page.content] // set a variable#[page.title:'Jinsoku Template Engine']
Unbuffered code for conditionals and anything else
[# var keywords = ['template', 'mvc', 'dom', 'node.js']; #] [# if (keywords.indexOf('mvc')) { #] Yahooo![# } else { #] Ooops![# } #]
Examples
We could combine some of the statements:
// main file // the article.html partial will be included here // article.html file #[article.title] #[article.description]
Extending
You can easily extend jinsoku by adding new or replace one of the existing parsers(if, case, each, for, var, evaluate).
To register a parser:
Jinsoku;
License
The MIT License
Copyright © 2013 D.G. Shogun147@gmail.com