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

Add functionality for dynamic includes #93 #156

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
25 changes: 20 additions & 5 deletions ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ require.relative = function (parent) {


require.register("ejs.js", function(module, exports, require){

/*!
* EJS
* Copyright(c) 2012 TJ Holowaychuk <tj@vision-media.ca>
Expand Down Expand Up @@ -209,18 +208,34 @@ var parse = exports.parse = function(str, options){
consumeEOL = true;
}

if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (0 == js.trim().indexOf('include')) {
var optionName = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');

// If the options provided to include is a variable in scope
// use the value of that variable.
var name = options[optionName] || optionName;
var path = resolveInclude(name, filename);
include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf += "' + (function(){" + include + "})() + '";
js = '';
}

while (~(n = js.indexOf("\n", n))) n++, lineno++;
if (js.substr(0, 1) == ':') js = filtered(js);

switch(js.substr(0, 1)) {
case ':':
js = filtered(js);
break;
case '%':
js = " buf.push('<%" + js.substring(1).replace(/'/g, "\\'") + "%>');";
break;
case '#':
js = "";
break;
}

if (js) {
if (js.lastIndexOf('//') > js.lastIndexOf('\n')) js += '\n';
buf += prefix;
Expand Down
2 changes: 1 addition & 1 deletion ejs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ var parse = exports.parse = function(str, options){
consumeEOL = true;
}

if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (0 == js.trim().indexOf('include')) {
var optionName = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');

// If the options provided to include is a variable in scope
// use the value of that variable.
var name = options[optionName] || optionName;
var path = resolveInclude(name, filename);
include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf += "' + (function(){" + include + "})() + '";
js = '';
}
Expand Down
6 changes: 6 additions & 0 deletions test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ describe('includes', function(){
.should.equal(fixture('include.css.html'));
})

it('should allow dynamic includes', function (){
var file = 'test/fixtures/dynamic1.ejs';
ejs.render(fixture('dynamic1.ejs'), {filename : file, dynamicTemplate : 'dynamic2'})
.should.equal(fixture('dynamic.html'));
});

it('should pass compileDebug to include', function(){
var file = 'test/fixtures/include.ejs';
var fn = ejs.compile(fixture('include.ejs'), { filename: file, open: '[[', close: ']]', compileDebug: false, client: true })
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/dynamic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span>An ejs template generated this</span>
<span>This was also generated by an ejs template</span>

2 changes: 2 additions & 0 deletions test/fixtures/dynamic1.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<span>An ejs template generated this</span>
<% include dynamicTemplate %>
1 change: 1 addition & 0 deletions test/fixtures/dynamic2.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>This was also generated by an ejs template</span>