Cannot render template using gulp-data AND passing options
Closed this issue · 5 comments
russellsamora commented
Goal is to use gulp-data in conjunction with template options to render template:
var gulp = require('gulp');
var data = require('gulp-data');
var template = require('gulp-template');
var path = require('path');
gulp.task('render-templates', function () {
return gulp.src('precompile-templates/*.html')
.pipe(data(function(file) {
return require('./data/' + path.basename(file.path, '.html') + '.json');
}))
.pipe(template({
interpolate: /{{([\s\S]+?)}}/g,
evaluate: /{=([\s\S]+?)=}/g
}))
.pipe(gulp.dest('dist'));
});
When using gulp data with NO options it works (verifying data works):
.pipe(template())
When using options with hard coded data it works (verifying options works):
.pipe(template({'name': 'russell'}, {
interpolate: /{{([\s\S]+?)}}/g,
evaluate: /{=([\s\S]+?)=}/g
}))
Is there some variable that has to be passed in the template function to reference the data?
sindresorhus commented
teemualap commented
Ok, I'll look into this. For now you can just pass an empty object as the first argument.
@sindresorhus: ok to assume that the data argument can be ignored if file.data is populated?
sindresorhus commented
Oh, I see it now. You're using it wrong. Should be:
.pipe(template(null, {
interpolate: /{{([\s\S]+?)}}/g,
evaluate: /{=([\s\S]+?)=}/g
}))
teemualap commented
Yes, in respect of the plugin api, that's how it should be used.
russellsamora commented
Thanks fellas, works like a charm.