3 Replies Latest reply on Aug 2, 2017 4:30 AM by Liam Dilley

    Looping nested items in module_json

    judemccarthy

      Hi there,

       

      I am wanting to  loop through nested items in module_json, but there must be an issue with my liquid statement as nothing is rendering.

       

      This is the liquid:

      {% for sport in this.sports %}

                <h3>{{this.sport}}</h3>

                     <table>

                          {% for event in this.events %}

                                   <tr><td>{{event.Name}}</td><td>{{event.code}}</td><td>{{event.Label}}</td></tr>

                         {% endfor %}

                </table>

      {% endfor %}

       

      This is the json:

      [{

      "sports": {

           "sport": "Badminton",

                "events": {

                     "event": [{

                     "Name": "Singles Women Open",

                     "code": "0302",

                     "Label": "2"

                         },

                          {

                     "Name": "Singles Men Open",

                     "code": "0303",

                     "Label": "2"

                     }

                ]

           }}

      },

      {

           "sport": "Basketball",

                "events": {

                          "event": [{

                          "Name": "Mixed Open",

                          "code": "771",

                          "Label": "1"

                          },

                          {

                          "Name": "Men Open",

                          code": "773",

                          "Label": "1"

                          }

                        ]

                }

              }

      ]

        • 1. Re: Looping nested items in module_json
          Robert K. Bell Level 4

          What is the output of {{ this.sports | json }} (before the for loop)?

           

          Also, the first line inside the for-loop should read <h3>{{ sport.sport }}</h3>. this doesn't change inside loops.

          • 2. Re: Looping nested items in module_json
            judemccarthy Level 1

            Hi Robert,

            {{ this.sports | json }} (

            The output before the loop is coming back with empty.

             

            Thanks fo your help, I might need to get someone to take a look at this issue.

             

            many thanks,

            Jude.

            • 3. Re: Looping nested items in module_json
              Liam Dilley Adobe Community Professional & MVP

              Hey Jude,

              This is an easy one.

              {% for sport in this.sports %}

                        <h3>{{this.sport}}</h3>

                             <table>

                                  {% for event in this.events %}

                                           <tr><td>{{event.Name}}</td><td>{{event.code}}</td><td>{{event.Label}}</td></tr>

                                 {% endfor %}

                        </table>

              {% endfor %}

               

              What you have here is the sport object, and event is part of the sport object. BUT you said this. There is no this.
              You also said this for the sport. But you declared "sport" as the object name.
              "this" is the collection you are targeting.

               

              {% for sport in this.sports %}

                        <h3>{{sport}}</h3>

                             <table>

                                  {% for event in sport.events %}

                                           <tr><td>{{sport.Name}}</td><td>{{sport.code}}</td><td>{{sport.Label}}</td></tr>

                                 {% endfor %}

                        </table>

              {% endfor %}