tengine
v0.0.6
Published
Unifying APIs of various of template engines.
Downloads
19
Maintainers
Readme
Tengine
Unifying APIs of various of template engines.
Installation
$ npm install tengine --save
Usage
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 );
} );
Supported template engines
- [x] doT (website) (example)
- [x] dust (website) (example)
- [ ] eco
- [ ] ect (website)
- [x] ejs (website) (example)
- [ ] haml
- [ ] haml-coffee
- [ ] handlebars (website)
- [ ] hogan (website)
- [ ] htmling
- [x] jade (website) (example)
- [ ] jazz
- [ ] jqtpl
- [ ] liquid (website)
- [x] lodash (website) (example)
- [ ] marko (website)
- [x] mustache (example)
- [x] nunjucks (website) (example)
- [ ] plates
- [ ] pug (website)
- [ ] ractive
- [ ] react
- [ ] slm
- [ ] swig-templates
- [ ] swig
- [ ] teacup
- [ ] templayed
- [ ] toffee
- [ ] twig
- [x] underscore (website) (example)
- [ ] vash
- [ ] walrus (website)
- [ ] whiskers
APIs
tengine( string name[, object engine ] )
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 );
tengine.support( string name )
to check if the template engine is supported in tengine.
- name: the name of the template engine
tengine.engines
An array of supported template engine's name.
engine.global
Global variables for templates.
const engine = require( 'underscore' );
engine.global = {
title : 'global title'
}
engine.renderString( '<%-title %>' ).then( res => {
console.log( res ); // output: global title
} );
engine.base
The base directory of template files
const engine = tengine( 'ejs' );
engine.base = '/path/to/ejs/template/directory';
engine.render( 'index.html' );
engine.engine
The template engine instance.
engins.path( string name )
To get the full path of a template file in base direcotry.
- name: the file name of the template file.
engine.context( object data )
To get the context data which will includes the global variables if exists.
- the local context for compiling the template.
engine.configure( object options )
Configuring the template engines.
engine.render( string file, object context )
Rendering a template file.
- file: the template file name.
- context: the context for the template
engine.renderString( string str, object context )
Rendering template string.
- str: the template string
- context: the context for compiling