在nunjucks中退出json对象时添加键值对

时间:2017-09-15 12:18:47

标签: node.js nunjucks gulp-nunjucks-render

我想用nunjucks模板在JSON对象中添加一个新的键值对,但无法添加它(不想添加任何自定义过滤器)。

假设我有一个对象

var obj = {"id": "1", "name": "test", "child": {"id": "2", "status": "true"}, 
           "template": "{{obj.child}}"}

现在我想添加一个像

这样的新密钥
{"template": "{% set json = obj.child %}{% set json.name = 'testing' %}"}

所以我想要的最终输出

{
  "id": "1",
  "name": "test",
  "child": {
    "id": "2",
    "status": "true"
  },
  "template": {
    "id": "2",
    "status": "true",
    "name": "testing"
  }
}

请让我怎么办?

0 个答案:

没有答案