简体   繁体   中英

Nunjucks initialize array in template

I'm trying to initialize an AmCharts data into nunjucks template. I pickup data from req.body and pass it as chartData

// server.js
var html = nunjucks.render("template.html", {
    chartData: req.body // chartData: JSON.stringify(req.body)
});

// template.html
am4core.ready(function () {

      chart = am4core.createFromConfig(config, 'chartdiv', am4charts.XYChart);

      // Add data
      chart.data = {{ chartData }};
});

I've tried the two approaches above (with and without stringify). But as result the chart data hasn't initialized. There some way to do initialize the template's "char.data" variable?

I solved the problem by other approach:

var data = req.body ;
var htmlData = fs.readFileSync(__dirname + file, 'utf8')
.replace("[{{data}}]", JSON.stringify(data));
var html = nunjucks.renderString(htmlData, {});

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