gulp-file-include icon indicating copy to clipboard operation
gulp-file-include copied to clipboard

Pass options to filters

Open andremalkine opened this issue 9 years ago • 6 comments

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.

andremalkine avatar Jul 28 '15 19:07 andremalkine

This should work

@@include( handlebars('./_tmpl.hbs', { foo: "bar" } ))

TrySound avatar Jul 28 '15 21:07 TrySound

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>

andremalkine avatar Jul 29 '15 02:07 andremalkine

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 avatar Jul 29 '15 02:07 andremalkine

@andremalkine This will be fixed soon. But not now, sorry.

TrySound avatar Jul 29 '15 06:07 TrySound

I'll keep my eye out. Thanks for looking into it!

andremalkine avatar Jul 29 '15 20:07 andremalkine

+1

JosefJezek avatar Sep 13 '15 19:09 JosefJezek