Unifying APIs of various of template engines.
$ npm install tengine --save
const path = require( 'path' );
const tengine = require( 'tengine' );
const engine = tengine( 'nunjucks' );
engine.configure( {
config : {
autoescape : false
},
filters : {
repeat : str => str + str
}
} );
engine.global = { engine : 'tengine' };
eigine.base = path.join( __dirname, 'templates' );
engine.render( 'index.html', {
title : 'tengine'
} ).then( res => {
console.log( res );
} );
- doT (website) (example)
- dust (website) (example)
- eco
- ect (website)
- ejs (website) (example)
- haml
- haml-coffee
- handlebars (website)
- hogan (website)
- htmling
- jade (website) (example)
- jazz
- jqtpl
- liquid (website)
- lodash (website) (example)
- marko (website)
- mustache (example)
- nunjucks (website) (example)
- plates
- pug (website)
- ractive
- react
- slm
- swig-templates
- swig
- teacup
- templayed
- toffee
- twig
- underscore (website) (example)
- vash
- walrus (website)
- whiskers
To initialize a template Engine
- name: the name of the template engine, see Supported template engines
- engine: to specify another template engine for replacing the default engine.
const engine = tengine( 'doT' );
const nunjucks = require( 'nunjucks' );
const engine = tengine( 'nunjucks', nunjucks );
to check if the template engine is supported in tengine.
- name: the name of the template engine
An array of supported template engine's name.
Global variables for templates.
const engine = require( 'underscore' );
engine.global = {
title : 'global title'
}
engine.renderString( '<%-title %>' ).then( res => {
console.log( res ); // output: global title
} );
The base directory of template files
const engine = tengine( 'ejs' );
engine.base = '/path/to/ejs/template/directory';
engine.render( 'index.html' );
The template engine instance.
To get the full path of a template file in base direcotry.
- name: the file name of the template file.
To get the context data which will includes the global variables if exists.
- the local context for compiling the template.
Configuring the template engines.
Rendering a template file.
- file: the template file name.
- context: the context for the template
Rendering template string.
- str: the template string
- context: the context for compiling