首页 > 解决方案 > 通过 Nunjucks Templating 在 json 对象中添加数据

问题描述

我在其中创建了数据对象和特征数组。我正在尝试通过 nunjucks 变量添加数据,但它显示模板渲染错误。

    mydata = {
         "type": "collection",
         "features" : []
       };


   {% for piece in data.pieces %}

      mydata.features.push({
                type: "Feature",
                properties: {
                  title: '{{ piece.title }}',
                  startDate: '{{ piece.startDate }}',
                  endDate: '{{ piece.endDate }}'
                },
                geometry: {
                  type: "Point",
                  coordinates: "abc"
                }
        });

  {% endfor %}

我的结果应该是这样的。

mydata = {
        "type": "collection",
        "features": [
                {
                 "type": "Feature", "properties": {"title": "", "startDate": "", "endDate": "" 
                },
         "geometry": { 
                "type": "Point","coordinates": [ ]
                } 

           ]
     }

标签: javascriptnode.jsapostrophe-cmsnunjucks

解决方案


推荐阅读