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

Pass elements of an array to include

Open morrislaptop opened this issue 9 years ago • 2 comments

The following doesn't seem to work?

@@for (var i = 0; i < posts.length; i++) {
            @@include('blog-tabs-list-item.html', posts[i])
          }

morrislaptop avatar Sep 26 '16 14:09 morrislaptop

well, while one wish a thing like that to work, in reality it's hell to program such a feature

yairEO avatar Dec 29 '16 21:12 yairEO

I know this is late but may be of use to others:

So, I sort of had the same requirement but did, eventually, arrive at a simple hack to get the result I need; merely pipe your stream into fileInclude multiple times. E.g.

gulp.src(whatever)
    .pipe(fileInclude({prefix: "@@", basepath: "@file"}))
    .pipe(fileInclude({prefix: "@@", basepath: "@file"}))
    .pipe(gulp.dest(wherever));

When running through a @@for, @@include statements will just been written into the output HTML directly instead of being 'executed'. But, provided the statement isn't malformed, there's nothing stopping us re-processing that HTML via fileInclude to execute them as many times as required. The number of 'repeat runs' will depend on how deep your nesting goes (I only have 1 level of nesting, so 1 repeat run serves my purposes).

N.B. in the above example, posts[i] will not work - the object must be encoded to a full JSON string for the next fileInclude pass to be able to parse it. Fortunately, this is relatively easy;

@@for (var i = 0; i < posts.length; i++) {
    @@include('blog-tabs-list-item.html', `+(JSON.stringify(posts[i]))+`)
}

MSMcBain avatar Mar 16 '17 08:03 MSMcBain