I am trying to create a layout from an array with Twig. My array has a depth of about seven nests and some keys have been dynamically added (so have different names) such as the time which is a direct child of "PLANNER" and "CLASS_" which has the ID appended to it.
"SCHEDULE": {
"DAY": "FRIDAY",
"PLANNER": {
"830": {
"CLASS_2231": {
"INSTRUCTOR": "TEACHER",
"CLASS_LEVEL": "PRIVATE",
"MAX_STUDENTS": "2",
"START_TIME": "830",
"FINISH_TIME": "900",
"STUDENTS": [
{
"STUDENT": {
"FIRST_NAME": "First Name",
"LAST_NAME": ""
}
},
{
"STUDENT": {
"FIRST_NAME": "First Name",
"LAST_NAME": ""
}
}
]
},
"CLASS_1959": {
"INSTRUCTOR": "TEACHER",
"CLASS_LEVEL": "PRIVATE",
"MAX_STUDENTS": "1",
"START_TIME": "830",
"FINISH_TIME": "900",
"STUDENTS": [
{
"STUDENT": {
"FIRST_NAME": "First Name",
"LAST_NAME": ""
}
}
]
}
},
"900": {
"CLASS_1742": {
"INSTRUCTOR": "TEACHER",
"CLASS_LEVEL": "DUCKLINGS",
"MAX_STUDENTS": "4",
"START_TIME": "900",
"FINISH_TIME": "930",
"STUDENTS": [
{
I have been trying to loop through this with the following code but it does seem to take variables (that have been set just before the next loop) as part of the for loop condition. However, it will print the variable in the nested loop (should I change the condition to let it run).
--EDITED
{% for key, value in schedule.SCHEDULE.PLANNER %}
<div class="scheduleTimeTab">
<p>{{ key }}</p>
</div>
{% set TIME = key %}
{% for key, value in schedule.SCHEDULE.PLANNER.TIME %}
<div class="innerSchedule">
<p>{{ 'IS IT WORKING??' }}</p>
</div>
{% endfor %}
{% endfor %}
I should also add that this code outputs this:
<div class="scheduleContainer">
<div class="scheduleTimeTab">
<p>830</p>
</div>
<div class="scheduleTimeTab">
<p>900</p>
</div>
</div>
Many thanks in advance Michael Z