如何将JSON数据传递到Nunjucks模板中?
问题描述:
我想使用Nunjucks模板,但想传入我自己的JSON数据以在模板上使用.
I want to use Nunjucks templates but want to pass in my own JSON data to be used on the templates.
这里的文档很少.
https://mozilla.github.io/nunjucks/templating.html
谢谢.
答
您可以使用 gulp-data 允许您将json文件传递到用于渲染Nunjucks的任务运行程序.
You can use gulp-data which allows you to pass json files to the task runner you're using to render Nunjucks.
gulp.task('nunjucks', function() {
return gulp.src('app/pages/**/*.+(html|nunjucks)')
// Adding data to Nunjucks
.pipe(data(function() {
return require('./app/data.json')
}))
.pipe(nunjucksRender({
path: ['app/templates']
}))
.pipe(gulp.dest('app'))
});