diff options
Diffstat (limited to 'lib/template/fs-loader.js')
-rw-r--r-- | lib/template/fs-loader.js | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/template/fs-loader.js b/lib/template/fs-loader.js index 00c4743..7f469bb 100644 --- a/lib/template/fs-loader.js +++ b/lib/template/fs-loader.js @@ -13,7 +13,7 @@ function isRelative(filename) { var Loader = nunjucks.Loader.extend({ init: function(searchPaths) { - this.searchPaths = searchPaths.map(path.normalize); + this.searchPaths = _.map(searchPaths, path.normalize); }, getSource: function(fullpath) { @@ -38,18 +38,37 @@ var Loader = nunjucks.Loader.extend({ }, resolve: function(from, to) { + var searchPaths = this.searchPaths; + // Relative template like "./test.html" if (isRelative(to) && from) { return path.resolve(path.dirname(from), to); } + // Determine in which search folder we currently are + var originalSearchPath = _.chain(this.searchPaths) + .sortBy(function(s) { + return -s.length; + }) + .find(function(basePath) { + return (from && from.indexOf(basePath) === 0); + }) + .value(); + var originalFilename = originalSearchPath? path.relative(originalSearchPath, from) : null; + + // If we are including same file from a different search path + // Slice the search paths to avoid including from previous ones + if (originalFilename == to) { + var currentIndex = searchPaths.indexOf(originalSearchPath); + searchPaths = searchPaths.slice(currentIndex + 1); + } + // Absolute template to resolve in root folder - var resultFolder = _.find(this.searchPaths, function(basePath) { + var resultFolder = _.find(searchPaths, function(basePath) { var p = path.resolve(basePath, to); return ( p.indexOf(basePath) === 0 - && p != from && fs.existsSync(p) ); }); |