From 78f836d4a23e2fb10d5af79de2333d55904c896c Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Fri, 29 Jan 2021 21:45:49 +0500 Subject: [PATCH 1/2] options.ext --- README.md | 1 + lib/ejs.js | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 009809c1..3a77ad20 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Therefore, we do not recommend using this shortcut. are using `renderFile()`. Used by `cache` to key caches, and for includes. - `root` Set project root for includes with an absolute path (e.g, /file.ejs). Can be array to try to resolve include from multiple directories. + - `ext` Extension, by default 'ejs' - `views` An array of paths to use when resolving includes with relative paths. - `context` Function execution context - `compileDebug` When `false` no debug instrumentation is compiled diff --git a/lib/ejs.js b/lib/ejs.js index 104aadad..f5496b61 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -115,14 +115,14 @@ exports.promiseImpl = (new Function('return this;'))().Promise; * @param {Boolean} [isDir=false] whether the parent file path is a directory * @return {String} */ -exports.resolveInclude = function(name, filename, isDir) { +exports.resolveInclude = function(name, filename, isDir, options) { var dirname = path.dirname; var extname = path.extname; var resolve = path.resolve; var includePath = resolve(isDir ? filename : dirname(filename), name); var ext = extname(name); if (!ext) { - includePath += '.ejs'; + includePath += (options && options.ext) ? '.'+options.ext : '.ejs'; } return includePath; }; @@ -170,7 +170,7 @@ function getIncludePath(path, options) { else { // Look relative to a passed filename first if (options.filename) { - filePath = exports.resolveInclude(path, options.filename); + filePath = exports.resolveInclude(path, options.filename, undefined, options); if (fs.existsSync(filePath)) { includePath = filePath; } @@ -526,6 +526,7 @@ function Template(text, opts) { options.cache = opts.cache || false; options.rmWhitespace = opts.rmWhitespace; options.root = opts.root; + options.ext = opts.ext; options.includer = opts.includer; options.outputFunctionName = opts.outputFunctionName; options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME; From 9daf9f1cad6a31c71e792245440e7f77068b1d75 Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Sun, 7 Feb 2021 21:03:50 +0500 Subject: [PATCH 2/2] options.ext --- lib/ejs.js | 2 +- test/ejs.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ejs.js b/lib/ejs.js index f5496b61..18cae6ef 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -163,7 +163,7 @@ function getIncludePath(path, options) { if (Array.isArray(options.root)) { includePath = resolvePaths(path, options.root); } else { - includePath = exports.resolveInclude(path, options.root || '/', true); + includePath = exports.resolveInclude(path, options.root || '/', true, options); } } // Relative paths diff --git a/test/ejs.js b/test/ejs.js index 5360499d..3f23d3a0 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -999,6 +999,12 @@ suite('include()', function () { fixture('include.html')); }); + test('include ejs with set ext', function () { + var viewsPath = path.join(__dirname, 'fixtures'); + assert.equal(ejs.render(fixture('include-root.ejs'), {}, {delimiter: '@', root:viewsPath, ext: 'html'}), + fixture('include.html')); + }); + test('include ejs with custom includer function', function () { var file = 'test/fixtures/include-root.ejs'; var inc = function (original, prev) {