Coder Social home page Coder Social logo

Data object overwritten about gulp-twig HOT 6 OPEN

simon-dt avatar simon-dt commented on September 15, 2024 3
Data object overwritten

from gulp-twig.

Comments (6)

jordanlev avatar jordanlev commented on September 15, 2024 1

I'm experiencing the same issue, and I think I have a workaround (until the bug is fixed). If you use gulp-foreach to compile each template in a separate process, it seems to fix the problem of variables "leaking" between templates. However, note that if you are also providing the 'data' option when you call gulp-twig, you must also copy/clone the object that you provide (otherwise the data continues to "leak" between templates, even if you're compiling them separately via gulp-foreach).

For example, before I had this in my gulpfile:

var gulp = require('gulp');
var twig = require('gulp-twig');

var data = {
  'something' : 123,
  'anotherThing': 'hello world'
};

gulp.task('twig', function() {
  gulp.src('/path/to/my/templates/**/*.twig')
    .pipe(twig({
      base: '/path/to/my/templates',
      data: data
    }))
    .pipe(gulp.dest('/my/output/path'));
});

...so I changed it to this (note that I'm using gulp-foreach and cloning the data object):

var gulp = require('gulp');
var twig = require('gulp-twig');
var foreach = require('gulp-foreach');

var data = {
  'something' : 123,
  'anotherThing': 'hello world'
};

gulp.task('twig', function() {
  gulp.src('/path/to/my/templates/**/*.twig')
    .pipe(foreach(function(stream, file) {
      return stream.pipe(twig({
        base: '/path/to/my/templates',
        data: JSON.parse(JSON.stringify(data))
      }));
    }))
    .pipe(gulp.dest('/my/output/path'));
});

I'm honestly not sure how/why exactly this works, but so far it seems to do the trick for me.

from gulp-twig.

olets avatar olets commented on September 15, 2024 1

You can use gulp-data for reliable handling of your JSON:

var gulp = require('gulp'),
    data = require('gulp-data'),
    foreach = require('gulp-foreach'),
    twig = require('gulp-twig');

// with data as a variable
var myData = {
    foo: 'bar',
    baz: 'qux'
}
gulp.task('twigWithExternalData', function() {
    return gulp.src('./src/**/*.twig')
        .pipe(data(myData))
        .pipe(foreach(function(stream, file) {
            return stream
                .pipe(twig())
        }))
        .pipe(gulp.dest('./dest/'));
});

// with data included directly
gulp.task('twigWithInternalData', function() {
    return gulp.src('./src/**/*.twig')
        .pipe(data({
            foo: 'bar',
            baz: 'qux'
        }))
        .pipe(foreach(function(stream, file) {
            return stream
                .pipe(twig())
        }))
        .pipe(gulp.dest('./dest/'));
});

from gulp-twig.

kluplau avatar kluplau commented on September 15, 2024

Did you get around this by any chance? I filed #22 which I think might be somehow related.

from gulp-twig.

ugomeda avatar ugomeda commented on September 15, 2024

I just added {% set title = null %} on the pages without title as a workaround.

from gulp-twig.

simon-dt avatar simon-dt commented on September 15, 2024

I'll have to look into this. Are you seeing the initial data object being mutaded by the twig templates themself?

That should not happen ...

Are you excluding the layout.twig from the src?

from gulp-twig.

derekrushforth avatar derekrushforth commented on September 15, 2024

Any progress on this? I'm running into the same issue.

from gulp-twig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.