请注意,循环的范围是Twig;因此声明了一个变量 在循环内部无法访问for循环内部。如果你 想要访问变量,只需在循环之前声明它。
有人可以解释为什么以下代码:
# Bundle/Resources/views/parent
{% set id = 5 %}
# Bundle/Resources/views/child
{% extends 'Bundle::parent.html.twig' %}
{%block one%}
{% for item in threeitemarray %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
{%endblock%}
{%block two%}
{% for item in threeitemarray %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
{%endblock%}
返回6,7,8,6,7,8而不是6,7,8,9,10,11?如何设置它以返回后者?
答案 0 :(得分:0)
试试这个
{% set id = 5 %}
{% for item in 0..3 %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
{% for item in 0..3 %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
答案 1 :(得分:0)
如果您的块也在父Twig中定义,我认为您不能这样做。如果情况并非如此,请尝试:
# Bundle/Resources/views/parent
{% set id = 5 %}
# Bundle/Resources/views/child
{% extends 'Bundle::parent.html.twig' %}
{% set id = id %}
{%block one%}
{% for item in threeitemarray %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
{%endblock%}
{%block two%}
{% for item in threeitemarray %}
{% set id = id + 1 %}
{{id}}
{%endfor%}
{%endblock%}