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

adding multi-root setting. If param root is an array of strings, #143

Merged
merged 1 commit into from
Dec 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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