Skip to content

Commit

Permalink
Provide support for absolute path partials
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Capper committed Jul 4, 2017
1 parent 894b0de commit d660db5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var join = path.join;
var resolve = path.resolve;
var extname = path.extname;
var dirname = path.dirname;
var isAbsolute = path.isAbsolute;

var readCache = {};

Expand Down Expand Up @@ -118,7 +119,19 @@ function readPartials(path, options, fn) {
function next(index) {
if (index === keys.length) return fn(null);
var key = keys[index];
var file = join(dirname(path), partials[key] + extname(path));
var partialPath = partials[key];

var file;
if (isAbsolute(partialPath)) {
if (extname(partialPath) !== '') {
file = partialPath;
} else {
file = join(partialPath + extname(path));
}
} else {
file = join(dirname(path), partialPath + extname(path));
}

read(file, options, function(err, str){
if (err) return fn(err);
options.partials[key] = str;
Expand Down
18 changes: 18 additions & 0 deletions test/shared/partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ exports.test = function(name) {
done();
});
});
it('should support absolute path partial', function(done){
var path = 'test/fixtures/' + name + '/partials.' + name;
var locals = {user: user, partials: {partial: __dirname + '/../../test/fixtures/' + name + '/user' }};
cons[name](path, locals, function(err, html){
if (err) return done(err);
html.should.equal('<p>Tobi</p>');
done();
});
});
it('should support relative path partial', function(done){
var path = 'test/fixtures/' + name + '/partials.' + name;
var locals = {user: user, partials: {partial: '../' + name + '/user' }};
cons[name](path, locals, function(err, html){
if (err) return done(err);
html.should.equal('<p>Tobi</p>');
done();
});
});
}
else {
it('should support rendering a partial', function(done){
Expand Down

0 comments on commit d660db5

Please sign in to comment.