haoxins / gulp-file-include

MAINTAINER WANTED ~ [gulp-file-include] a gulp plugin for file include
MIT License
680 stars 96 forks source link

Pass elements of an array to include #119

Open morrislaptop opened 8 years ago

morrislaptop commented 8 years ago

The following doesn't seem to work?

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

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

FrontierFox commented 7 years ago

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]))+`)
}