(3/4) Nested For(loops) in Twig gives some issues


#1

Hi,

I am trying to loop over data in Twig. The first two loops there is not a problem but with the thrid is really don’t understand why it won;t work. I recieve the data with multiple joins from the database.

Array: (page.accommodations)

{"id":3, "accommodations":[{"id":1, "name":"One","acco_type":2, "accommodation_type": 
{"id":2,"name":"acco type 2",},
"accommodation_rooms":[
{"id":1,"name":"Room 1 of Acco One ",
"room_type":
{"id":2,"name":"two persons"}
},
{"id":3,"name":"Room 2 of Acco One",
"room_type":
{"id":3,"name":"three persons room"}
}]
}

In Twig:

{% for item in page['accommodations'] %}
<li>Accommodation: {{item.id}}, {{item.name}} - {{ item.accommodation_type.name }}</li>

    <ul><strong>Rooms:</strong>
        {% for room in item['accommodation_rooms'] %}
            <li>{{ room.name }},                  // gives name (works)
               <br>{{ room.id }},                 // gives id (works)
               <br>{{ room.room_type }},          // gives id (works but don't know how...)

                {% for type in room.room_type %}
                       {{ type.name }},     // nothing...
                {% endfor %}
             </li>
        {% endfor %}
     </ul>
{% endfor %}

Could not find if there is a maximum for (nested) loops. Really have no clue what is (not) happening here. Hope someone does!


#2

I’ve never come across a limit when nesting loops in Twig. A couple of things come to mind to check. Are you sure {{ room.room_type }} is giving you the id and not something else like the number of child elements? Second, the array you posted above looks a little odd. There are two opening [ but only one closing ]. Also after "name":"acco type 2" there is a comma, but no additional property.

{  
   "id":3,
   "accommodations":[  
      {  
         "id":1,
         "name":"One",
         "acco_type":2,
         "accommodation_type":{  
            "id":2,
            "name":"acco type 2",

         },
         "accommodation_rooms":[  
            {  
               "id":1,
               "name":"Room 1 of Acco One ",
               "room_type":{  
                  "id":2,
                  "name":"two persons"
               }
            },
            {  
               "id":3,
               "name":"Room 2 of Acco One",
               "room_type":{  
                  "id":3,
                  "name":"three persons room"
               }
            }
         ]
      }

#3

Hi Tim,

I did make the array smaller for reading purposes.

About " is giving you the id and not something else like the number of child elements? ". Yes, Got the Id from the Array.

P.s. this is the relation.

->with('accommodations.accommodation_type', 'accommodations.accommodation_rooms.room_type') 

#4

There is no max in loops.

What you could do, if you doubt the for-loops:
Use {{loop.index ]}in the outher loop and the inner loop and check if the counter shows the number of records you expect.
I think the devil is in the details:
Your data is not respresenting the fields you mention, so it is harder for us to guess what is wrong.

{{ room.room_type }} should NOT give you an id because in your data example, it is an array
{{ room.room_type.id }} should.

did you know you can use {{ dump() }}?

So perhaps {{dump(room}} will tell you more about the content, so you could check if the content is the expected one.