mirror of
https://github.com/CryptKeeperZK/ejs.git
synced 2026-01-10 07:58:26 -05:00
Added Promise support with capabilities test to renderFile
This commit is contained in:
60
lib/ejs.js
60
lib/ejs.js
@@ -220,13 +220,32 @@ function handleCache(options, template) {
|
||||
|
||||
function tryHandleCache(options, data, cb) {
|
||||
var result;
|
||||
try {
|
||||
result = handleCache(options)(data);
|
||||
if (!cb) {
|
||||
if (typeof Promise == 'function') {
|
||||
return new Promise(function (resolve, reject) { // eslint-disable-line no-undef
|
||||
try {
|
||||
result = handleCache(options)(data);
|
||||
resolve(result);
|
||||
}
|
||||
catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
throw new Error('Please provide a callback function');
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
return cb(err);
|
||||
else {
|
||||
try {
|
||||
result = handleCache(options)(data);
|
||||
}
|
||||
catch (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
cb(null, result);
|
||||
}
|
||||
return cb(null, result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,17 +418,27 @@ exports.render = function (template, d, o) {
|
||||
*/
|
||||
|
||||
exports.renderFile = function () {
|
||||
var filename = arguments[0];
|
||||
var cb = arguments[arguments.length - 1];
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var filename = args.shift();
|
||||
var cb;
|
||||
var opts = {filename: filename};
|
||||
var data;
|
||||
|
||||
if (arguments.length > 2) {
|
||||
data = arguments[1];
|
||||
|
||||
// No options object -- if there are optiony names
|
||||
// in the data, copy them to options
|
||||
if (arguments.length === 3) {
|
||||
// Do we have a callback?
|
||||
if (typeof arguments[arguments.length - 1] == 'function') {
|
||||
cb = args.pop();
|
||||
}
|
||||
// Do we have data/opts?
|
||||
if (args.length) {
|
||||
// Should always have data obj
|
||||
data = args.shift();
|
||||
// Normal passed opts (data obj + opts obj)
|
||||
if (args.length) {
|
||||
// Use shallowCopy so we don't pollute passed in opts obj with new vals
|
||||
utils.shallowCopy(opts, args.pop());
|
||||
}
|
||||
// Special casing for Express (opts-in-data)
|
||||
else {
|
||||
// Express 4
|
||||
if (data.settings) {
|
||||
if (data.settings.views) {
|
||||
@@ -424,11 +453,6 @@ exports.renderFile = function () {
|
||||
utils.shallowCopyFromList(opts, data, _OPTS_EXPRESS);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Use shallowCopy so we don't pollute passed in opts obj with new vals
|
||||
utils.shallowCopy(opts, arguments[2]);
|
||||
}
|
||||
|
||||
opts.filename = filename;
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user