mengine
v0.1.0
Published
Use multiple template engines with ease
Downloads
3
Readme
mengine
men-gine, it has nothing to do with gender. Men comes from the dutch verb mennen which means lead.
Motivation
I wanted a package that allowed me to swap template engines with minimal configuration and changes. After looking at the consolidate package code I had a few questions.
Why is the template name method the one that renders a file?
Why does the render method renders a string template?
Why isn't it possible to use the template engine directly?
Why are all the methods in one file?
The answer to these questions lead me to create a new package.
The things that are important for this package are:
- making the switch to another language as easy as possible
- allow the user to be able to (allmost) all the power template enigne provides
Usage
var engine = require('mengine')('ejs');
The engine
variable exposes:
- engine: the template engine instance
- renderString: it the template engine supports asynchrone actions
- renderStringSync: if the template engine supports synchrone actions
- renderFile: it the template engine supports asynchrone actions
- renderFileSync: if the template engine supports synchrone actions
Mengine has a second parameter which allows you to add or overwrite the template engine configuration.
config/template_engine/ejs.js
var ejs = require('ejs');
module.exports = function ejsConfig(){
ejs.open = '<%';
ejs.close = '%>'
var out = {};
out.engine = ejs;
out.renderFile = ejs.renderFile;
out.renderString = function(str, options, fn){
try{
fn(null, ejs.render(str, options));
}catch(er){
fn(err);
}
};
return out;
};
The call from a root file would be;
var engine = require('mengine')('ejs', './config/template/ejs');
If there is no custom configuration file mengine will search for a configuration file in the lib/config directory. The following template engines are supported:
- atpl+
- eco
- ect * +
- ejs * -
- haml-coffee -
- haml
- handlebars -
- hogan * +
- jade +
- jazz
- just * +
- liquor
- lodash *
- mustache * -
- nunjucks +
- qejs * +
- ractive * -
- swig +
- templayed
- toffee +
- underscore *
- walrus
- whiskers -
If the template language allows the change of the open and close strings, marked above with *, the package configuration will set them to ${ and } respectively.
Template languages marked with + and - allow subtemplates. The ones with - only have partials. The ones with + have inheritance and partials.
Tests
Look in the test directory for more documentation.
Run the tests after installing the package with npm test
.