简体   繁体   中英

How can you pass JSON data into a Nunjucks file?

I have a .njk file that I would like to populate with data from a JSON file.

Currently I am using Webpack.

Here is my JSON File:

{
    "ranking": "Colors",
    "description": "Here is a ranking of my favorite colors",
    "rankings": [
        {
            "rank": 1,
            "institution": "red",
        },
        {
            "rank": 2,
            "institution": "Blue",
        },
        {
            "rank": 3,
            "institution": "Green",
        }
    ]
}

In my nunjucks file, what would be the syntax I would need to use to pass in this data?

The code which worked for me:

var json1 = require('./data/data-1.json');

const SolutionTemplate = require('./solution.njk');

let toggleSolutionFunc = function() {
      if (document.getElementById('solution-content') != null) {
        document.getElementById('solution-content').innerHTML = SolutionTemplate.render(json1); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM