gulp-file-include
gulp-file-include copied to clipboard
Pass options to filters
I wanted to set up a handlebars template filter, which I did like so: html:
@@include(handlebars('./_tmpl.hbs'))
gulpfile:
gulp.task('html', function() {
return gulp.src( project.html )
.pipe(fileinclude({
prefix: '@@',
basepath: '@file',
filters: {
handlebars: function(content){
var template = handlebars.compile(content);
return template();
}
}
}))
.pipe(gulp.dest( path.dist ));
});
It works great. However, I would love to be able to specify the data I want to run through handlebars in the @@include
statement, like so:
@@include( handlebars('./_tmpl.hbs'), { foo: "bar" } )
or
@@include( handlebars('./_tmpl.hbs', { foo: "bar" } ))
or even
@@include( handlebars('./_tmpl.hbs', '../data/foo.json'))
The point being, to be able to pass an option into the filter handler. I guess this would only make sense if you were writing your own handler and not just providing some kind of parser like markdown.parse
, but it would open up a lot of possibilities.
This should work
@@include( handlebars('./_tmpl.hbs', { foo: "bar" } ))
As far as I can tell, it does not. To clarify, the point is to get the { foo: "bar" }
object into the filter handler in the gulpfile, ie:
index.html:
@@include( handlebars('./_tmpl.hbs', { foo: "bar" } ))
_tmpl.hbs:
<p>This is a template: {{foo}}!</p>
gulpfile.js
gulp.task('html', function() {
return gulp.src( project.html )
.pipe(fileinclude({
prefix: '@@',
basepath: '@file',
filters: {
handlebars: function( includedFileContent, options ){
// Where `options` is the { foo: "bar" } object
var template = handlebars.compile(content);
return template(options);
}
}
}))
.pipe(gulp.dest( path.dist ));
});
Desired output html:
<p>This is a template: bar!</p>
Ultimately I probably wouldn't pass JSON directly like that, but rather specify a path to a data file that the gulp task would require and then feed to the handlebars template.
@andremalkine This will be fixed soon. But not now, sorry.
I'll keep my eye out. Thanks for looking into it!
+1