Skip to content

Commit

Permalink
Fixes mde#358 and others -- fixes options-passing for Express
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Apr 19, 2018
1 parent ca2c30f commit aaa00de
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ var _DEFAULT_DELIMITER = '%';
var _DEFAULT_LOCALS_NAME = 'locals';
var _NAME = 'ejs';
var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
var _OPTS = ['delimiter', 'scope', 'context', 'debug', 'compileDebug',
var _OPTS_PASSABLE_WITH_DATA = ['delimiter', 'scope', 'context', 'debug', 'compileDebug',
'client', '_with', 'rmWhitespace', 'strict', 'filename'];
// We don't allow 'cache' option to be passed in the data obj
// for the normal `render` call, but this is where Express puts it
// We don't allow 'cache' option to be passed in the data obj for
// the normal `render` call, but this is where Express 2 & 3 put it
// so we make an exception for `renderFile`
var _OPTS_EXPRESS = _OPTS.concat('cache');
var _OPTS_PASSABLE_WITH_DATA_EXPRESS = _OPTS_PASSABLE_WITH_DATA.concat('cache');
var _BOM = /^\uFEFF/;

/**
Expand Down Expand Up @@ -408,7 +408,7 @@ exports.render = function (template, d, o) {
// No options object -- if there are optiony names
// in the data, copy them to options
if (arguments.length == 2) {
utils.shallowCopyFromList(opts, data, _OPTS);
utils.shallowCopyFromList(opts, data, _OPTS_PASSABLE_WITH_DATA);
}

return handleCache(opts, template)(data);
Expand All @@ -433,6 +433,7 @@ exports.renderFile = function () {
var cb;
var opts = {filename: filename};
var data;
var viewOpts;

// Do we have a callback?
if (typeof arguments[arguments.length - 1] == 'function') {
Expand All @@ -447,21 +448,28 @@ exports.renderFile = function () {
// 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)
// Special casing for Express (settings + opts-in-data)
else {
// Express 4
// Express 3 and 4
if (data.settings) {
// Pull a few things from known locations
if (data.settings.views) {
opts.views = data.settings.views;
}
if (data.settings['view cache']) {
opts.cache = true;
}
// Undocumented after Express 2, but still usable, esp. for
// items that are unsafe to be passed along with data, like `root`
viewOpts = data.settings['view options'];
if (viewOpts) {
utils.shallowCopy(opts, viewOpts);
}
}
// Express 3 and lower
else {
utils.shallowCopyFromList(opts, data, _OPTS_EXPRESS);
}
// Express 2 and lower, values set in app.locals, or people who just
// want to pass options in their data. NOTE: These values will override
// anything previously set in settings or settings['view options']
utils.shallowCopyFromList(opts, data, _OPTS_PASSABLE_WITH_DATA_EXPRESS);
}
opts.filename = filename;
}
Expand Down

0 comments on commit aaa00de

Please sign in to comment.