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 include from absolute path and var #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,28 @@ var parse = exports.parse = function(str, options){
consumeEOL = true;
}

if (0 == js.trim().indexOf('include')) {
if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');
var path = resolveInclude(name, filename);
// If it is not a path, but a variable name
var indexes = name.split('.'),
lenIndexes = indexes.length,
indexno = 0,
ref = options[indexes[0]];
while(ref) {
indexno++;
if(indexno == lenIndexes) break;
ref = ref[indexes[indexno]];
}
if(ref && isAbsolutePath(ref))
var path = resolveInclude(ref); // absolute path from var
else if(ref)
var path = resolveInclude(ref, filename); // relative path from var
else if(isAbsolutePath(name))
var path = resolveInclude(name); // absolute path
else
var path = resolveInclude(name, filename); // relative path

include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf += "' + (function(){" + include + "})() + '";
Expand Down Expand Up @@ -327,8 +345,13 @@ exports.renderFile = function(path, options, fn){
* @api private
*/

function isAbsolutePath(path) {
return (path[0] === '/' || /^[\S]+:[\S]+/.test(path));
}

function resolveInclude(name, filename) {
var path = join(dirname(filename), name);
// if(relative path) ... else ...
var path = (typeof filename !== 'undefined') ? join(dirname(filename), name) : name;
var ext = extname(name);
if (!ext) path += '.ejs';
return path;
Expand Down