简体   繁体   English

如何将JSON数据传递给Nunjucks文件?

[英]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. 我有一个.njk文件,我想用JSON文件中的数据填充。

Currently I am using Webpack. 目前我正在使用Webpack。

Here is my JSON File: 这是我的JSON文件:

{
    "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? 在我的nunjucks文件中,我需要使用什么语法来传递这些数据?

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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