sindresorhus/gulp-template

Cannot render template using gulp-data AND passing options

Closed this issue · 5 comments

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?

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?

Oh, I see it now. You're using it wrong. Should be:

.pipe(template(null, {
            interpolate: /{{([\s\S]+?)}}/g,
            evaluate:    /{=([\s\S]+?)=}/g
        }))

Yes, in respect of the plugin api, that's how it should be used.

Thanks fellas, works like a charm.