Avoid caching when watching/compiling html templates with BrowserSync, etc.
.pipe(swig({defaults:{ cache:false}}))
** NEW **
Inject data into your templates via the newgulp-dataplugin. It creates afile.dataproperty with the data you need. This new method makes it much easier and less restrictive for getting data, than the methods below it. I'd recommend using this new method.
If you've created a template calledhomepage.htmlyou can create a JSON file calledhomepage.jsonto contain any variables you want injected into the template.
var swig =require('gulp-swig');
var opts ={
load_json:true
};
gulp.task('templates',function(){
gulp.src('./lib/*.html')
.pipe(swig(opts))
.pipe(gulp.dest('./dist/'))
});
Inject variables from both a data object and JSON file into templates
var swig =require('gulp-swig');
var opts ={
load_json:true,
data:{
headline:"Welcome"
}
};
gulp.task('templates',function(){
gulp.src('./lib/*.html')
.pipe(swig(opts))
.pipe(gulp.dest('./dist/'))
});
By default, gulp-swig will look for the json data file in the same location as the template. If you have your data in a different location, there's an option for that:
var swig =require('gulp-swig');
var opts ={
load_json:true,
json_path:'./data/',
data:{
headline:"Welcome"
}
};
gulp.task('templates',function(){
gulp.src('./lib/*.html')
.pipe(swig(opts))
.pipe(gulp.dest('./dist/'))
});
Inject variables using theSwig::setDefaultsmethod, and set other swig defaults.
Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:
The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIONOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.