Comments (6)
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.
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.
Did you get around this by any chance? I filed #22 which I think might be somehow related.
from gulp-twig.
I just added {% set title = null %}
on the pages without title as a workaround.
from gulp-twig.
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.
Any progress on this? I'm running into the same issue.
from gulp-twig.
Related Issues (20)
- How to exclude files? HOT 4
- fails tests when using twigjs 1.10.4 HOT 2
- Support for Null-coalescing operator HOT 1
- Add me as a contributor HOT 10
- ‘set’ variable causes a bug HOT 7
- json_encode with constant
- bringing releases up to date HOT 8
- data variable reference bug HOT 2
- include TwigException HOT 7
- {{ dump() }} crash gulp HOT 2
- Does not return valid json
- Correct syntax when using with gulp-data? HOT 3
- How to use Base option properly with Extends, Block, Include and Import tags? HOT 1
- twig conditions error
- Actual example of gulp-data use
- Passing twig options
- Error compiling twig template
- How to use function "source"? HOT 1
- include .html with variables go wrong
- Is it possible to generate random dummy text, e.g. using lorem? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gulp-twig.