mincer
Version:
Web assets processor. Native JavaScript port of Sprockets.
66 lines (50 loc) • 1.39 kB
JavaScript
/**
* class EjsEngine
*
* Engine for the EJS compiler. You will need `ejs` Node module installed
* in order to use [[Mincer]] with `*.ejs` files:
*
* npm install ejs
*
* This is a mixed-type engine that can be used as a 'backend' of [[JstEngine]]
* as well as standalone 'middleware' processor in a pipeline.
*
*
* ##### SUBCLASS OF
*
* [[Template]]
**/
'use strict';
// 3rd-party
var ejs; // initialized later
// internal
var Template = require('../template');
////////////////////////////////////////////////////////////////////////////////
// Class constructor
var EjsEngine = module.exports = function EjsEngine() {
Template.apply(this, arguments);
ejs = ejs || Template.libs.ejs || require('ejs');
};
require('util').inherits(EjsEngine, Template);
// Render data
EjsEngine.prototype.evaluate = function (context, locals) {
if (this.nextProcessor && this.nextProcessor.name === 'JstEngine') {
this.data = ejs.compile(this.data, {
filename: context.logicalPath,
client: true
}).toString();
return;
}
if (ejs.VERSION) { // ejs 2.x.x
this.data = ejs.render(this.data, locals, {
filename: context.pathname,
context: context
});
} else { // ejs 1.x.x
this.data = ejs.render(this.data, {
filename: context.pathname,
scope: context,
locals: locals
});
}
};