提供数据模型以在Gulp Nunjucks模板中使用 [英] Providing data models to use in Gulp Nunjucks templates
问题描述
我正在使用Gulp来渲染我的Nunjucks模板,使用 gulp-nunjucks 或 gulp-nunjucks-render 。有没有一种方法可以将一个或一系列.json文件传递给模板包以在我的nunjucks模板中使用JSON数据?
I'm looking to use Gulp to render my Nunjucks templates with either gulp-nunjucks or gulp-nunjucks-render. Is there a way I can pass one or a series of .json files to the templating package to use the JSON data in my nunjucks templates?
理想情况下,我会有一个 models /
目录,每个页面都有相应的page.json文件,内容将在该模板中使用。
Ideally I'd have a models/
directory with each page having corresponding page.json file with contents to be used in that template.
I我想知道上述任何一个插件是否可行,以及如何实现。任何单个或一系列.json文件的例子都是非常有用的。
I'd like to know if it's possible with either of the above plugins and if so how it can be implemented. Any examples for a single or series of .json files would be very useful.
推荐答案
使用gulp-data https://www.npmjs.org/package/gulp-data 它可以从任何来源生成JSON ,它可以是一个JSON文件或数据库,通过文件对象(file.data)上的一个新属性将它发送到流中。您的nunjucks插件需要修改才能使用该数据属性。
Look into using gulp-data https://www.npmjs.org/package/gulp-data it produces JSON from a any source, be it a JSON file, or database, that sends it down the stream via a new attribute on the file object (file.data). Your nunjucks plugin will need to be modified to consume that data property.
这篇关于提供数据模型以在Gulp Nunjucks模板中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!