这其实是两个问题...
我有一个 SWIG 模板 (index.html),我想将多个 JSON 文件拉入其中以使用 Node.js 进行编译。一个“index.json”文件,其中包含仅与该页面相关的变量,然后是一个“common.json”文件,其中包含一组我想在整个系统中使用的公共变量。
然后我在“index.html”中有一个“header.html”模板和一个“footer.html”模板。我如何让他们分别拉出自己的“header.json”和“footer.json”文件?
最终,我试图让这一切在 GULP-SWIG 中工作,因为我们已经有一个 GULP 进程在项目的其余部分始终运行。
更新: GULP-SWIG 自动查找具有相同名称的 JSON 文件并对其进行处理,但没有关于包含其他 JSON 文件的文档。
我试过这样:
// Include gulp
var gulp = require('gulp');
// Include Our Plugins
var swig = require('gulp-swig');
// Swig Variables
var common = require('./json/common.json'); // <--- NEW CODE
var optEng = {
load_json: true,
json_path: 'json/',
data: {
locale: 'en_US',
currencyval: 'USD'
}
};
// Tasks
gulp.task('swig-eng', function() {
gulp.src('templates/*.html')
.pipe(swig(common)) // <--- NEW CODE
.pipe(swig(optEng))
.pipe(gulp.dest('./compiled/'));
});
gulp.task('watch', function() {
gulp.watch('templates/*.html', ['swig-eng']);
gulp.watch('includes/*.html', ['swig-eng']);
gulp.watch('json/*.json', ['swig-eng']);
});
gulp.task('build', ['swig-eng', 'watch']);
我这样尝试过:
// Include gulp
var gulp = require('gulp');
// Include Our Plugins
var swig = require('gulp-swig');
// Swig Variables
var optEng = {
common: require('./json/common.json'), // <--- NEW CODE
load_json: true,
json_path: 'json/',
data: {
locale: 'en_US',
currencyval: 'USD'
}
};
// Tasks
gulp.task('swig-eng', function() {
gulp.src('templates/*.html')
.pipe(swig(optEng))
.pipe(gulp.dest('./compiled/'));
});
gulp.task('watch', function() {
gulp.watch('templates/*.html', ['swig-eng']);
gulp.watch('includes/*.html', ['swig-eng']);
gulp.watch('json/*.json', ['swig-eng']);
});
gulp.task('build', ['swig-eng', 'watch']);
我创建了一个包含所需文件结构的 ZIP 文件: https ://www.dropbox.com/s/psxsdn31rd5177h/Gulp-Swig%20Sample.zip
gulpfile.js 文件是唯一需要更新的文件。