L
L
Leonid Markov2019-08-21 15:33:13
JSON
Leonid Markov, 2019-08-21 15:33:13

How to output data from local .json file to Pug markup?

I have a task - to lay out a page on Pug and insert data from a local data.json file into certain places in the markup. And I don’t understand how to integrate it into the .pug file - somehow through include or use require ? Or maybe there is another way? And then no matter how much I googled - all the infa comes across some kind of contradictory, and in the docks on Pug there is nothing at all on this topic

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Zachinalov, 2019-08-21
@masterbiz

gulpfile.js

const Gulp = require('gulp');
const Pug = require('gulp-pug');
const Fs = require('fs');

const dataFromFile = JSON.parse(Fs.readFileSync('./data.json'))

// Pug to HTML
Gulp.task('pug', () =>
    Gulp.src('./*.pug')
    .pipe(Pug({
        pretty: true,
        locals: dataFromFile || {}
    }))
    .pipe(Gulp.dest('./build'))
);

data.json
{
  "sections": {
    "html": {
      "name": "HTML",
      "color": "#e44d26"
    }
  }
}

*.pug
or
.test #{sections.html.color}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question