Skip to content

Commit

Permalink
Merge 84d4cf5 into 3270f27
Browse files Browse the repository at this point in the history
  • Loading branch information
backflip committed Jan 26, 2017
2 parents 3270f27 + 84d4cf5 commit a0456e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ gulp.task('i18n', function () {
- `parseHelperName` `{Function(options, file): String}`
- `decorators` `{String|Array.<String>|Object|Function(handlebars)}`
- `parseDecoratorName` `{Function(options, file): String}`
- `data` `{String|Array.<String>|Object}`
- `data` `{String|Array.<String>|Object|Function(file)}`
- `parseDataName` `{Function(options, file): String}`
Returns a Gulp-compatible transform stream that compiles [Handlebars][handlebars] templates to static output.
Expand Down
6 changes: 5 additions & 1 deletion src/gulp-hb.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function gulpHb(options) {
wax.decorators(options.decorators);
}

if (options.data) {
if (options.data && typeof options.data !== 'function') {
wax.data(options.data);
}

Expand All @@ -81,6 +81,10 @@ function gulpHb(options) {
var data = assign({}, file.data);
var template = wax.compile(file.contents.toString());

if (options.data && typeof options.data === 'function') {
wax.data(options.data(file));
}

if (debug) {
logKeys(file, [
['context', wax.context],
Expand Down
32 changes: 32 additions & 0 deletions test/gulp-hb.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ test.cb('should use registered data', t => {
}));
});

test.cb('should use file-specific registered data', t => {
const stream = gulpHb({
data: function (file) {
return {
foo: file.path
};
}
});

stream.data({
bar: 'barA'
});

stream.on('data', file => {
t.is(file.contents.toString(), file.path + ' ' + file.path + ' barA');
});

stream.write(new gutil.File({
base: __dirname,
path: path.join(__dirname, 'fixture', 'fixture.js'),
contents: new Buffer('{{@root.foo}} {{foo}} {{bar}}')
}));

stream.write(new gutil.File({
base: __dirname,
path: path.join(__dirname, 'fixture', 'fixture2.js'),
contents: new Buffer('{{@root.foo}} {{foo}} {{bar}}')
}));

setImmediate(t.end);
});

test.cb('should use registered partial', t => {
const stream = gulpHb({
partials: {
Expand Down

0 comments on commit a0456e6

Please sign in to comment.