diff --git a/Readme.md b/Readme.md index ab0a3dd1..077bd4ec 100644 --- a/Readme.md +++ b/Readme.md @@ -65,6 +65,16 @@ Embedded JavaScript templates. <% include user/show %> <% }) %> +``` + + Starting with "/" will drive you to the absolute path. + +``` + ``` ## Custom delimiters diff --git a/ejs.js b/ejs.js index ab44a80c..146da7cb 100644 --- a/ejs.js +++ b/ejs.js @@ -207,7 +207,7 @@ var parse = exports.parse = function(str, options){ 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); + var path = resolveInclude(name, filename, options.baseDir); include = read(path, 'utf8'); include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug }); buf += "' + (function(){" + include + "})() + '"; @@ -311,6 +311,7 @@ var compile = exports.compile = function(str, options){ * - `locals` Local variables object * - `cache` Compiled functions are cached, requires `filename` * - `filename` Used by `cache` to key caches + * - `baseDir` Used by `resolveInclude` to absolute paths * - `scope` Function execution context * - `debug` Output generated function body * - `open` Open tag, defaulting to "<%" @@ -379,8 +380,10 @@ exports.renderFile = function(path, options, fn){ * @api private */ -function resolveInclude(name, filename) { - var path = join(dirname(filename), name); +function resolveInclude(name, filename, baseDir) { + name = name.replace(/(\'|\")/ig,""); + if (name[0] === '/') var absolute = (baseDir || process.cwd()); + var path = join((absolute || dirname(filename)), name); var ext = extname(name); if (!ext) path += '.ejs'; return path; diff --git a/lib/ejs.js b/lib/ejs.js index f36944fb..2901e5f2 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -155,7 +155,7 @@ var parse = exports.parse = function(str, options){ 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); + var path = resolveInclude(name, filename, options.baseDir); include = read(path, 'utf8'); include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug }); buf += "' + (function(){" + include + "})() + '"; @@ -259,6 +259,7 @@ var compile = exports.compile = function(str, options){ * - `locals` Local variables object * - `cache` Compiled functions are cached, requires `filename` * - `filename` Used by `cache` to key caches + * - `baseDir` Used by `resolveInclude` to absolute paths * - `scope` Function execution context * - `debug` Output generated function body * - `open` Open tag, defaulting to "<%" @@ -327,8 +328,10 @@ exports.renderFile = function(path, options, fn){ * @api private */ -function resolveInclude(name, filename) { - var path = join(dirname(filename), name); +function resolveInclude(name, filename, baseDir) { + name = name.replace(/(\'|\")/ig,""); + if (name[0] === '/') var absolute = (baseDir || process.cwd()); + var path = join((absolute || dirname(filename)), name); var ext = extname(name); if (!ext) path += '.ejs'; return path; diff --git a/test/ejs.js b/test/ejs.js index 7df7ace9..f7893762 100644 --- a/test/ejs.js +++ b/test/ejs.js @@ -264,7 +264,7 @@ describe('includes', function(){ .should.equal(fixture('include.html')); }) - it('should work when nested', function(){ + it('should work when nested or absolute', function(){ var file = 'test/fixtures/menu.ejs'; ejs.render(fixture('menu.ejs'), { filename: file, pets: users }) .should.equal(fixture('menu.html')); diff --git a/test/fixtures/menu.ejs b/test/fixtures/menu.ejs index 61fad418..7fbc4c41 100644 --- a/test/fixtures/menu.ejs +++ b/test/fixtures/menu.ejs @@ -8,4 +8,8 @@ <% var url = '/baz' -%> <% var title = 'Baz' -%> -<% include includes/menu-item -%> \ No newline at end of file +<% include includes/menu-item -%> + +<% var url = '/boz' -%> +<% var title = 'Boz' -%> +<% include "/test/fixtures/includes/menu-item" -%> diff --git a/test/fixtures/menu.html b/test/fixtures/menu.html index 1f9e45fd..68c305ab 100644 --- a/test/fixtures/menu.html +++ b/test/fixtures/menu.html @@ -1,3 +1,4 @@
  • Foo
  • Bar
  • -
  • Baz
  • \ No newline at end of file +
  • Baz
  • +
  • Boz
  • \ No newline at end of file