Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options.ext #577

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/ejs.js
Expand Up @@ -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;
};
Expand Down Expand Up @@ -163,14 +163,14 @@ 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
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;
}
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/ejs.js
Expand Up @@ -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) {
Expand Down