Pass random value to gulp pipe template

user254694

I have a gulp task

gulp.task('markdown', function () {
    gulp.src('content/*.md')
        .pipe(newer('assembler/markdown_out'))
        .pipe(markdown())
        .pipe(wrap({ src: 'assembler/markdowntemp/wrapper.html'}))
        .pipe(fileinclude({
            prefix: '@@',
            basepath: '@file'
        }))
        .pipe(template({classname: getClassName()}))
        .pipe(gulp.dest('assembler/markdown_out'));
});

and the function getClassName is

function getClassName(){
 var classnames_size = ["big", "medium", "small", "extrabig"];

 var classShape = ["tvscreen", "oval"];

 return classnames_size[Math.floor(Math.random()*classnames_size.length)] + " " + classShape[Math.floor(Math.random()*classShape.length)];

}

unfortunately when I run this task it looks like the template only gets compiled once, whereas I want something where the template gets compiled for every file being passed in, so that I can generate the random classnames.

Is there an option to set it to compile each time, or a different plugin that will do that.

avcajaraville

The main problem you have with your approach is that gulp uses "a glob and represents a file structure" (gulp official documentation).

So this is how it works: it will take all files in the stream, process them and send them to the next pipe "in a row".

Thats why your function getClassName gets executed only one time.

I order to accomplish what you want, you must think about manipulate each file independently, and thus, you will loose "power" with gulp.

Once you have each file path, you can run your task (now a function) over this file. The task basically will extract the route of each file in the stream and call the functioncompileMarkdown, which is your "old" gulp task. Now this will be executed one time per file, thus, your getClassName function will be executed one time for each file, and thus generate the "random class" (didnt dig on that function, I rely on the fact that is working properly).

This example uses the following plugin: https://www.npmjs.org/package/glob-to-vinyl/ in order to process each files from the stream.

Here is the code, hope it helps !

var gulp = require('gulp'),
    globToVinyl = require('glob-to-vinyl');

gulp.task('markdown', function() {
  globToVinyl('content/*.md', function(err, files){
    for (var file in files) {
      compileMarkdown(files[file].path);
    }
  });
});

function compileMarkdown(file) {
  gulp.src('file')
    .pipe(newer('assembler/markdown_out'))
    .pipe(markdown())
    .pipe(wrap({ src: 'assembler/markdowntemp/wrapper.html'}))
    .pipe(fileinclude({
        prefix: '@@',
        basepath: '@file'
    }))
    .pipe(template({classname: getClassName()}))
    .pipe(gulp.dest('assembler/markdown_out'));
}

function getClassName(){
  var classnames_size = ["big", "medium", "small", "extrabig"];
  var classShape = ["tvscreen", "oval"];
  return classnames_size[Math.floor(Math.random()*classnames_size.length)] + " " + classShape[Math.floor(Math.random()*classShape.length)];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass random value to gulp pipe template

From Dev

Gulp Task - Pass var to next pipe

From Dev

Pass value through template

From Dev

gulp.on('data') How to pass data to next pipe

From Dev

How can I pass gulp.watch() to a pipe?

From Dev

Observable with Async Pipe in template is not working for single value

From Dev

How to pass a value to Jade template

From Dev

pass value from ngOnInit to template

From Dev

Gulp pipe minimal function?

From Dev

gulp - how to pipe tasks

From Dev

Gulp condition inside pipe

From Dev

gulp - how to pipe tasks

From Dev

Jade: how can I pass a random variable into included template?

From Dev

How to pass value to read variable with pipe (stdin) in bash

From Dev

sane way to pass/keep a value throughout a long pipe

From Dev

Pass form value in Laravel to Blade template

From Dev

Django how to pass value from template to view

From Dev

how to pass value attribute in laravel template blading?

From Dev

Gulp pipe to separate destination folders

From Dev

Gulp / Unhandled stream error in pipe

From Dev

How to create repeating pipe in gulp?

From Dev

Gulp plumber cannot pipe to undefined

From Dev

gulp - make gulp.pipe(gulp.dest) sync

From Dev

Append to a pipe and pass on?

From Dev

Pass arguments with pipe in bash

From Dev

Gulp pass in files to process

From Dev

Pass a value from parent directive template to child directive

From Dev

It is possible pass the value of an id from a template, to function (AngulasJS2)

From Dev

pass value from EditorTemplate to its Layout Template only, in mvc 6