Answer the question
In order to leave comments, you need to log in
How to get data from different JSON for different pages and language versions (Nunjucks)?
Torturing Nunjucks.
There is a task to make up pages 20-30 and in two languages (ru, eng).
I want to get data from json with the following structure:
source
---data
--- ---pages
--- --- ---ru
--- --- --- ---index.json
--- --- --- ---catalog.json
--- --- --- ---contact.json (ит.д.)
--- --- ---en
--- --- --- ---index.json
--- --- --- ---catalog.json
--- --- --- ---contact.json (ит.д.)
--- --- global-ru.json
--- --- global-en.json
const gulp = require("gulp");
const data = require("gulp-data");
const nunjucksRender = require("gulp-nunjucks-render");
// HTML (Nunjacks)
const njkHTML = () => {
let versionNumber = Number(new Date().toISOString().replace(/\D+/g, ""));
return gulp.src("source/*.njk")
.pipe(plumber())
.pipe(data(function () {
return [require("./source/data/global-ru.json"),
require("./source/data/pages/ru/index.json")];
}))
/* с таким return вообще не получает данные */
/* работает только когда 1 файл */
.pipe(nunjucksRender({
manageEnv: (env) => {
env.opts.autoescape = false;
env.addGlobal(`template`, `source/layouts/default.njk`);
env.addGlobal(`versionNumber`, versionNumber);
}
}))
.pipe(gulp.dest("build"));
};
exports.njkHTML = njkHTML;
Answer the question
In order to leave comments, you need to log in
Here https://www.npmjs.com/package/gulp-data
The first example is "Get data via JSON file, keyed on filename."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question