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

Correct syntax when using with gulp-data? #53

Closed j-greig closed 6 years ago

j-greig commented 6 years ago

Loving this Gulp/Twig thing :)

One question... I'm loading some data from an external file which works fine, but it's now compiling to .twig files instead of .html which I don't want.

How do I set the extname to .html inside the compile function AND call the external json file?

(Sorry new to Gulp and can't figure out the syntax)


var swig = require('gulp-swig');
var data = require('gulp-data');
var livereload = require('gulp-livereload');
var fs = require('fs');

gulp.task('compile', function () {
    'use strict';
    var twig = require('gulp-twig');
    return gulp.src(['./src/*.twig', '!./src/_data.json', '!./src/layout.twig'])
      .pipe(data(function(file) {
        return JSON.parse(fs.readFileSync('./src/_data.json'));
      }))
      .pipe(gulp.dest('./web'))
      .pipe(livereload());
});

gulp.task('watch', function() {
  livereload.listen();
  gulp.watch('./src/**/*.twig', ['compile']);
  gulp.watch('./src/_data.json', ['compile']);
});```
j-greig commented 6 years ago

Ha, figured it out right after posting this 🦆

In case it helps someone else...

var swig = require('gulp-swig');
var data = require('gulp-data');
var livereload = require('gulp-livereload');
var fs = require('fs');

gulp.task('compile', function () {
    'use strict';
    var twig = require('gulp-twig');
    return gulp.src(['./src/*.twig', '!./src/_data.json', '!./src/layout.twig'])
      .pipe(data(function(file) {
        return JSON.parse(fs.readFileSync('./src/_data.json'));
      }))
      .pipe(twig({
            extname: '.html'
        }))
      .pipe(gulp.dest('./web'))
      .pipe(livereload());
});

gulp.task('watch', function() {
  livereload.listen();
  gulp.watch('./src/**/*.twig', ['compile']);
  gulp.watch('./src/_data.json', ['compile']);
});

gulp.task('default', ['watch']);
olets commented 6 years ago

Glad you figured it out 👍

Stackoverflow's the best place for getting up to speed on Gulp. But since we're here, a couple quick thoughts on your Gulpfile:

j-greig commented 6 years ago

Wow, thanks for the pointers! Wasn't expecting that : )