0

I have created data object and features array in it. i am trying to add data through nunjucks variables but it shows Template render error.

    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 %}

My result should be like this.

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

           ]
     }
  • 3
    You're not actually executing the code inside your for loop. In Nunjucks, you can't just write vanilla JavaScript like that - you have to use their template methods (like {% set varName = "test" %} to set a variable). There are also a lot of methods that you can't use in Nunjucks templates - one of them is "push" on an array. Instead, you should create a helper in one of your Apostrophe-CMS modules that processes the data for you. Take a look here for an example: https://stackoverflow.com/questions/46722204/apostrophe-widget-helpers – Joseph Sep 04 '19 at 05:21
  • Joseph, i suggest that you post your response as an answer so it can be accepted. – Tom Boutell Sep 08 '19 at 20:08

0 Answers0