H
H
HelpSophie2020-09-23 15:00:38
HTML
HelpSophie, 2020-09-23 15:00:38

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


So far, I can only get data from one json file .

There are several questions:
1. How to make the desired page learn about its json?

2. So far I have an idea how to solve it in 2 separate gulp tasks. Can they be somehow cleverly combined into one task? And does it make sense

Here is my task:
my gulp task

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;



If there is anything else you need to clarify, please tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2020-09-24
@HelpSophie

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 question

Ask a Question

731 491 924 answers to any question