• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Looping nested items in module_json

New Here ,
Aug 01, 2017 Aug 01, 2017

Copy link to clipboard

Copied

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"

                    }

                  ]

          }

        }

]

TOPICS
Web apps

Views

520

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 01, 2017 Aug 01, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines