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

adding "include function" feature to ejs. #1

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ function Template(text, opts) {
}

this.opts = options;
this.rootTemplate = opts.rootTemplate || this;
this.global = {};

this.regex = this.createRegex();
}
Expand Down Expand Up @@ -555,6 +557,7 @@ Template.prototype = {
compile: function () {
var src;
var fn;
var self = this;
var opts = this.opts;
var prepended = '';
var appended = '';
Expand All @@ -563,14 +566,21 @@ Template.prototype = {

if (!this.source) {
this.generateSource();
prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
prepended += ' var __output = [], __append = function() { var target = __global.__outputStack[0]; return target.push.apply(target, arguments); }' + '\n';
if (opts.client) {
// TODO: support options.client + include function
prepended += ' __global = __global || {}' + '\n';
}
prepended += ' __global.__outputStack = __global.__outputStack || [];' + '\n';
prepended += ' __global.__outputStack.splice(0, 0, __output);' + '\n';
if (opts.outputFunctionName) {
prepended += ' var ' + opts.outputFunctionName + ' = __append;' + '\n';
}
if (opts._with !== false) {
prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
appended += ' }' + '\n';
}
appended += ' __global.__outputStack.splice(0, 1);' + '\n';
appended += ' return __output.join("");' + '\n';
this.source = prepended + this.source + appended;
}
Expand Down Expand Up @@ -623,7 +633,7 @@ Template.prototype = {
else {
ctor = Function;
}
fn = new ctor(opts.localsName + ', escapeFn, include, rethrow', src);
fn = new ctor(opts.localsName + ', escapeFn, include, rethrow, __global', src);
}
catch(e) {
// istanbul ignore else
Expand All @@ -643,6 +653,7 @@ Template.prototype = {
}

if (opts.client) {
fn.template = this;
fn.dependencies = this.dependencies;
return fn;
}
Expand All @@ -651,15 +662,28 @@ Template.prototype = {
// created by the source-code, with the passed data as locals
// Adds a local `include` function which allows full recursive include
var returnedFn = function (data) {
var include = function (path, includeData) {
var include = function (includePathOrFunction, includeData) {
var d = utils.shallowCopy({}, data);
if (includeData) {
d = utils.shallowCopy(d, includeData);
}
return includeFile(path, opts)(d);

if (typeof includePathOrFunction === 'function') {
var src = ' var __includeOutput = [];' + '\n'
+ (opts.client ? ' __global = __global || {};' + '\n' : '')
+ ' __global.__outputStack.splice(0, 0, __includeOutput);' + '\n'
+ ' ' + (opts.async ? 'await ' : '') + '__includeFunction(data);' + '\n'
+ ' __global.__outputStack.splice(0, 1);' + '\n'
+ ' return __includeOutput.join("");';
return new ctor('__includeFunction, data, __global', src)(includePathOrFunction, d, self.rootTemplate.global);
} else {
opts.rootTemplate = self.rootTemplate;
return includeFile(includePathOrFunction, opts)(d);
}
};
return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow]);
return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow, self.rootTemplate.global]);
};
returnedFn.template = this;
returnedFn.dependencies = this.dependencies;
return returnedFn;
},
Expand Down
4 changes: 4 additions & 0 deletions test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ suite('preprocessor include', function () {
throw new Error('expected inclusion error');
});

test('supports including an function instead of a file path (blocks feature)', function () {
assert.equal(ejs.render(fixture('blocks_implementation.ejs'), {}, {filename: path.join(__dirname, 'blocks_implementation.ejs')}),
fixture('blocks_result.html'));
});
});

suite('comments', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/blocks_implementation.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% let content = () => { %>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
<% } %>
<%- include('./fixtures/blocks_layout2', { content, title: 'Blocks For EJS', data: 233 }) %>
8 changes: 8 additions & 0 deletions test/fixtures/blocks_layout1.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title><%= title%></title>
</head>
<body>
<%- include(content)%>
</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/blocks_layout2.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% let layout2Content = () => { -%>
<h2><%= title%></h2>
<main>
<%- include(content)%>
</main>
<% } -%>
<%- include('blocks_layout1', { content: layout2Content, title }) %>
18 changes: 18 additions & 0 deletions test/fixtures/blocks_result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<html>
<head>
<title>Blocks For EJS</title>
</head>
<body>
<h2>Blocks For EJS</h2>
<main>

<ul>
<li>foo</li>
<li>bar</li>
</ul>

</main>

</body>
</html>