Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #143 from nka11/multi-root
Browse files Browse the repository at this point in the history
adding multi-root setting. If param root is an array of strings,
  • Loading branch information
paularmstrong committed Dec 8, 2012
2 parents dd24370 + bd2411a commit 6adf103
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 26 additions & 3 deletions lib/swig.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,32 @@ exports.compileFile = function (filepath, forceAllowErrors) {
}

get = function () {
var file = ((/^\//).test(filepath) || (/^.:/).test(filepath)) ? filepath : _config.root + '/' + filepath,
data = fs.readFileSync(file, config.encoding);
tpl = getTemplate(data, { filename: filepath });
var excp,
getSingle,
c;
getSingle = function (prefix) {
var file = ((/^\//).test(filepath) || (/^.:/).test(filepath)) ? filepath : prefix + '/' + filepath,
data;
try {
data = fs.readFileSync(file, config.encoding);
tpl = getTemplate(data, { filename: filepath });
} catch (e) {
excp = e;
}
};
if (typeof _config.root === "string") {
getSingle(_config.root);
}
if (_config.root instanceof Array) {
c = 0;
while (tpl === undefined || c < _config.root.length) {
getSingle(_config.root[c]);
c = c + 1;
}
}
if (tpl === undefined) {
throw excp;
}
};

if (_config.allowErrors || forceAllowErrors) {
Expand Down
9 changes: 8 additions & 1 deletion tests/node/swig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ describe('swig.compileFile', function () {
expect(swig.compileFile('included_2.html').render({ array: [1, 1] }))
.to.equal('2');
});

it('accepts an array in root config', function () {
swig.init({
root: ["/", __dirname + '/templates'],
allowErrors: true
});
expect(swig.compileFile('included_2.html').render({ array: [1, 1] }))
.to.equal('2');
});
it('can use an absolute path', function () {
swig.init({
root: __dirname + '/templates',
Expand Down

0 comments on commit 6adf103

Please sign in to comment.