tengine

Unifying APIs of various of template engines.

Usage no npm install needed!

<script type="module">
  import tengine from 'https://cdn.skypack.dev/tengine';
</script>

README

Tengine

Unifying APIs of various of template engines.

Build Status Coverage Status Node Version License NPM Version NPM Download Greenkeeper badge

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

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