Skip to content

Commit 941b37c

Browse files
authored
Merge pull request #515 from leomp12/feat/includer-funcion
Feat/ Includer funcion
2 parents 1be9d90 + 6e21968 commit 941b37c

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ Therefore, we do not recommend using this shortcut.
9090
output inside scriptlet tags.
9191
- `async` When `true`, EJS will use an async function for rendering. (Depends
9292
on async/await support in the JS runtime.
93+
- `includer` Custom function to handle EJS includes, receives `(originalPath, parsedPath)`
94+
parameters, where `originalPath` is the path in include as-is and `parsedPath` is the
95+
previously resolved path. Should return an object `{ filename, template }`,
96+
you may return only one of the properties, where `filename` is the final parsed path and `template`
97+
is the included content.
9398

9499
This project uses [JSDoc](http://usejsdoc.org/). For the full public API
95100
documentation, clone the repository and run `npm run doc`. This will run JSDoc

lib/ejs.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function getIncludePath(path, options) {
179179
if (!includePath && Array.isArray(views)) {
180180
includePath = resolvePaths(path, views);
181181
}
182-
if (!includePath) {
182+
if (!includePath && typeof options.includer !== 'function') {
183183
throw new Error('Could not find the include file "' +
184184
options.escapeFunction(path) + '"');
185185
}
@@ -307,6 +307,17 @@ function fileLoader(filePath){
307307
function includeFile(path, options) {
308308
var opts = utils.shallowCopy({}, options);
309309
opts.filename = getIncludePath(path, opts);
310+
if (typeof options.includer === 'function') {
311+
var includerResult = options.includer(path, opts.filename);
312+
if (includerResult) {
313+
if (includerResult.filename) {
314+
opts.filename = includerResult.filename;
315+
}
316+
if (includerResult.template) {
317+
return handleCache(opts, includerResult.template);
318+
}
319+
}
320+
}
310321
return handleCache(opts);
311322
}
312323

@@ -515,6 +526,7 @@ function Template(text, opts) {
515526
options.cache = opts.cache || false;
516527
options.rmWhitespace = opts.rmWhitespace;
517528
options.root = opts.root;
529+
options.includer = opts.includer;
518530
options.outputFunctionName = opts.outputFunctionName;
519531
options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME;
520532
options.views = opts.views;

test/ejs.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,36 @@ suite('include()', function () {
999999
fixture('include.html'));
10001000
});
10011001

1002+
test('include ejs with custom includer function', function () {
1003+
var file = 'test/fixtures/include-root.ejs';
1004+
var inc = function (original, prev) {
1005+
if (original.charAt(0) === '/') {
1006+
return {
1007+
filename: path.join(__dirname, 'fixtures', prev)
1008+
};
1009+
} else {
1010+
return prev;
1011+
}
1012+
};
1013+
assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}),
1014+
fixture('include.html'));
1015+
});
1016+
1017+
test('include ejs with includer returning template', function () {
1018+
var file = 'test/fixtures/include-root.ejs';
1019+
var inc = function (original, prev) {
1020+
if (prev === '/include.ejs') {
1021+
return {
1022+
template: '<p>Hello template!</p>\n'
1023+
};
1024+
} else {
1025+
return prev;
1026+
}
1027+
};
1028+
assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}),
1029+
fixture('hello-template.html'));
1030+
});
1031+
10021032
test('work when nested', function () {
10031033
var file = 'test/fixtures/menu.ejs';
10041034
assert.equal(ejs.render(fixture('menu.ejs'), {pets: users}, {filename: file}),

test/fixtures/hello-template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hello template!</p>

0 commit comments

Comments
 (0)