simon-dt / gulp-twig

Twig plugin for gulp.js, The streaming build system. Looking for maintainer or collaborators. See wiki
https://github.com/zimmen/gulp-twig/wiki/Looking-for-maintainer-or-collaborator(s)
MIT License
62 stars 33 forks source link

Pipe modified templates by passing file.contents into twigOpts.data #30

Closed signal-intrusion closed 6 years ago

signal-intrusion commented 8 years ago

This gulp task does not allow us to pipe in files where we have changed the template in a previous gulp task. For example, we have templates with front matter that we remove (check out the example in the gulp-data repo), but the front matter is still rendered.

Passing the file contents as the data property to twigOpts. twig.js will use the string in file.data when you call Twig.twig(twigOpts). Check out the twig.js source where it renders a template from params.data: https://github.com/twigjs/twig.js/blob/master/src/twig.exports.js#L41-L48

This would allow gulp tasks to, in effect, change templates before rendering them. It would also speed up rendering because it will use buffered data, avoiding an expensive call to Twig.loadRemoteContent() fetching templates from the filesystem.

      var Twig = require('twig'),
            twig = Twig.twig,
            twigOpts = {
                path: file.path,
                async: false
            },
            template;

        if (!!useFileContents) {
            twigOpts.data = String(file.contents);
        }
bezoerb commented 6 years ago

https://github.com/zimmen/gulp-twig/releases/tag/v1.2.0