Permalink
Browse files

allow rendering templates from an absolute path

  • Loading branch information...
1 parent debdd2c commit 0ed2710fcf5dfd0651cac7db05e3a5a4c144aac7 @paularmstrong committed Sep 27, 2011
Showing with 12 additions and 6 deletions.
  1. +2 −5 index.js
  2. +10 −1 tests/index.test.js
View
@@ -93,10 +93,6 @@ function createTemplate(data, id) {
return template;
}
-/*
-* Returns a template object from the given filepath.
-* The filepath needs to be relative to the template directory.
-*/
exports.fromFile = function (filepath) {
if (filepath[0] === '/') {
filepath = filepath.substr(1);
@@ -107,7 +103,8 @@ exports.fromFile = function (filepath) {
}
var get = function () {
- var data = fs.readFileSync(config.root + '/' + filepath, config.encoding);
+ var file = ((/^\//).test(filepath)) ? filepath : config.root + '/' + filepath,
+ data = fs.readFileSync(file, config.encoding);
CACHE[filepath] = createTemplate(data, filepath);
};
View
@@ -14,7 +14,6 @@ exports.fromFile = testCase({
var tpl = swig.fromFile('included_2.html');
test.strictEqual('2', tpl.render({ array: [1, 1] }), 'from file is a-ok');
-
test.done();
},
@@ -38,5 +37,15 @@ exports.fromFile = testCase({
}, 'throws when allowErrors is true');
test.done();
+ },
+
+ 'absolute path': function (test) {
+ swig.init({
+ root: __dirname + '/foobar',
+ allowErrors: true
+ });
+ var tpl = swig.fromFile('/' + __dirname + '/templates/included_2.html');
+ test.strictEqual('2', tpl.render({ array: [1, 1] }), 'file from absolute path is a-ok');
+ test.done();
}
});

0 comments on commit 0ed2710

Please sign in to comment.