Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 细枝中存在错误的父循环上下文_Loops_Symfony_Twig - Fatal编程技术网

Loops 细枝中存在错误的父循环上下文

Loops 细枝中存在错误的父循环上下文,loops,symfony,twig,Loops,Symfony,Twig,我用的是symfony2.3和twig1.15。我有一个嵌套的foreach-twig循环,我试图在外循环的最后一次迭代中得到一个不同的结果 我看到了: 但是,我得到了一个不同的结果-在访问父上下文的行中出现错误: Key "loop" for array with keys "groups, scores, type, user, assetic, app, avatarsDir, sonata_block, _parent, _seq, group, _key, subgroup" does

我用的是symfony2.3和twig1.15。我有一个嵌套的foreach-twig循环,我试图在外循环的最后一次迭代中得到一个不同的结果

我看到了: 但是,我得到了一个不同的结果-在访问父上下文的行中出现错误:

Key "loop" for array with keys "groups, scores, type, user, assetic, app, avatarsDir, sonata_block, _parent, _seq, group, _key, subgroup" does not exist in "(...)"
去除类、ID和不必要标记的相关代码:

{% for group in groups %}
    <div>
        {% for subgroup in group.subgroups %}
            {% for test in subgroup.tests %}
                {% block test_block_box %}
                    {% if not loop.parent.loop.last %}

                        (html follows...)

                    {% else %}

                        (some different html follows...)

                    {% endif %}
                {% endblock %}
            {% endfor %}
        {% endfor %}
    </div>
{% endfor %}
我已确保错误不涉及内部循环调用,即我将loop.parent.loop.last替换为loop.last,页面成功呈现的内容明显错误,但没有崩溃

当访问父上下文时,我做错了什么???

只要删除{%block test\u block\u box%}和{%endblock%},循环父上下文应该是可访问的

您可以尝试在{%block%}之外定义一个值,并查看您是否有权在{%block%}中访问该值:


你能在循环中不使用{%block%}吗?也许Twig在确定块中的父对象时遇到问题。是的,这解决了问题。显然,block指令改变了Twig的上下文。你知道这是否是预期的行为吗?我最终就是这么做的。谢谢:
{% for group in groups %}
    <div>
        {% for subgroup in group.subgroups %}
            {% for test in subgroup.tests %}
                {% set test_loop_is_last = loop.last %} {# define the value #}
                {% block test_block_box %}
                    {% if not test_loop_is_last %}{#test the value #}

                        (html follows...)

                    {% else %}

                        (some different html follows...)

                    {% endif %}
                {% endblock %}
            {% endfor %}
        {% endfor %}
    </div>
{% endfor %}