A
A
aslanov_shm2018-12-24 16:16:59
Pug
aslanov_shm, 2018-12-24 16:16:59

How to get data from json file in pug template?

Have a file structure

|--blocks
   |--block.pug
   |--block.json

blocks.pug
ul.blocks
  each link, title in blocksData
    li.blocks__item
      a(href="link")= title

blocks.json
{
  "blocksData": {
    "Ссылка 1" : "/link1",
    "Ссылка 2" : "/link2",
    "Ссылка 3" : "/link3"
  }
}

How can I get data from json into pug template without the help of Gulp? Inclusion or what?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
monochromer, 2018-12-24
@aslanov_shm

json must first be converted to data outside the template engine

var pug = require('pug');
var fn = pug.compileFile('path to pug file', options);
var locals = require('./block.json');
var html = fn(locals);

The template engine should not implement the data retrieval logic, its task is to display the data.

L
leskovoleg, 2019-07-26
@leskovoleg

var pug = require('pug');
var fn = pug.compileFile('path to pug file', options);
var html = fn({'req': require});

=====
//pug file

- var menu = req('./block.json')

ul.blocks
        each link, title in menu.blocksData
               li.blocks__item
                        a(href="link")= title

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question