From 51ca07ae6dbadcc6e502dcd207b348814dedfb36 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 12 Mar 2014 10:20:47 +0100 Subject: [PATCH] Add include from absolute path and var --- lib/ejs.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/ejs.js b/lib/ejs.js index f36944fb..6244d61f 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -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 + "})() + '"; @@ -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;