Twig - 我可以在for循环条件中使用变量吗?

时间:2017-11-17 07:22:44

标签: php symfony loops for-loop twig

我正在尝试使用Twig从数组创建布局。我的数组有大约七个巢的深度,并且一些键已被动态添加(因此具有不同的名称),例如时间是“PLANNER”和“CLASS_”的直接子项,其中附加了ID。

"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": [
                    {

我一直在尝试使用以下代码来遍历这个代码,但它确实将变量(已经在下一个循环之前设置)作为for循环条件的一部分。但是,它会在嵌套循环中打印变量(我应该更改条件以使其运行)。

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

我还应该补充一点,这段代码输出:

<div class="scheduleContainer">
    <div class="scheduleTimeTab">
        <p>830</p>
    </div>
    <div class="scheduleTimeTab">
        <p>900</p>
    </div>
</div>

非常感谢提前 迈克尔Z

0 个答案:

没有答案