summaryrefslogtreecommitdiffstats
path: root/lib/template/fs-loader.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/template/fs-loader.js')
-rw-r--r--lib/template/fs-loader.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/template/fs-loader.js b/lib/template/fs-loader.js
index 5720cb5..00c4743 100644
--- a/lib/template/fs-loader.js
+++ b/lib/template/fs-loader.js
@@ -7,12 +7,18 @@ var nunjucks = require('nunjucks');
Nunjucks loader similar to FileSystemLoader, but avoid infinite looping
*/
+function isRelative(filename) {
+ return (filename.indexOf('./') === 0 || filename.indexOf('../') === 0);
+}
+
var Loader = nunjucks.Loader.extend({
init: function(searchPaths) {
this.searchPaths = searchPaths.map(path.normalize);
},
getSource: function(fullpath) {
+ if (!fullpath) return null;
+
fullpath = this.resolve(null, fullpath);
if(!fullpath) {
@@ -26,7 +32,18 @@ var Loader = nunjucks.Loader.extend({
};
},
+ // We handle absolute paths ourselves in ".resolve"
+ isRelative: function() {
+ return true;
+ },
+
resolve: function(from, to) {
+ // Relative template like "./test.html"
+ if (isRelative(to) && from) {
+ return path.resolve(path.dirname(from), to);
+ }
+
+ // Absolute template to resolve in root folder
var resultFolder = _.find(this.searchPaths, function(basePath) {
var p = path.resolve(basePath, to);
@@ -36,7 +53,7 @@ var Loader = nunjucks.Loader.extend({
&& fs.existsSync(p)
);
});
-
+ if (!resultFolder) return null;
return path.resolve(resultFolder, to);
}
});