简体   繁体   English

在node express中将对象传递给nunjucks模板

[英]passing an object to nunjucks template in node express

I am passing an object in Express to a Nunjucks template 我将Express中的对象传递给Nunjucks模板

app.get('/purchase', function (req, res) {

  purchase_data = JSON.stringify(req.query);
  res.render('purchase', {"purchase": purchase_data});

})

------------------------

<ul>
  {% for key,value in purchase %}
    <li>{{key}} | {{value}}</li>
  {% endfor %}
</ul>

The output is literally each and every letter of the value property. 输出实际上是value属性的每个字母。 For example: {"quantity": "1"} becomes 0 | 例如:{“quantity”:“1”}变为0 | { 1 | {1 | " 2 | q 3 | u 4 | a 5 | n 6 | t 7 | i 8 | t 9 | y 10 | " 11 | “2 | q 3 | u 4 | a 5 | n 6 | t 7 | i 8 | t 9 | y 10 |”11 | : 12 | :12 | " 13 | 1 14 | " “13 | 1 14 |”

Not that experiences with nunjucks, and for that matter express, but this is a common enough task. 不是那种与nunjucks的经历,并且就此而言,但这是一项非常普遍的任务。 In nudge in the right direction would be very much appreciated. 在正确的方向推动将非常感激。

I am passing an object in Express to a Nunjucks template 我将Express中的对象传递给Nunjucks模板

No, you're not. 不你不是。 You're passing a string: 你传递一个字符串:

purchase_data = JSON.stringify(req.query);           // make a string
res.render('purchase', {"purchase": purchase_data}); // pass the string to the template

Instead, just pass the object as-is: 相反,只需按原样传递对象:

res.render('purchase', { purchase : req.query });

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

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