sindresorhus / gulp-template

Render/precompile Lodash templates
MIT License
289 stars 77 forks source link

Cannot render template using gulp-data AND passing options #14

Closed russellsamora closed 10 years ago

russellsamora commented 10 years ago

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 10 years ago

@teemualap

teemualap commented 10 years ago

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 10 years ago

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 10 years ago

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

russellsamora commented 10 years ago

Thanks fellas, works like a charm.