我在树枝上有一个嵌套的循环,我得到了1,1,1,1 / 2,2,2,2 / 3,3,3,3形式的元素加倍。如何使用第一个循环从第二个循环访问数据而不重复元素?
{% for form in forms %}
{% for question in surveyQuestions %}
<div class="overlay-container tab-content">
<div role="tabpanel" class="show-rendered-form tab-pane in fade active" id="show-rendered-form">
{% form_theme form with ['bootstrap_3_horizontal_layout.html.twig'] %}
{{ form(form) }}
<div class="overlay-mask">
<div class="btn-group">
<a class="edit-action btn btn-default" href="#show-edit-form"
data-question-id="{{ question.id }}" aria-controls="show-rendered-form"
role="tab" data-toggle="tab">Editeaza</a>
<a class="delete-action btn btn-default" href="#show-rendered-form"
data-question-id="{{ question.id }}" aria-controls="" role="tab"
data-toggle="tab">Sterge</a>
</div>
</div>
</div>
<div role="tabpanel" class="show-edit-form tab-pane" id="show-edit-form">
</div>
</div>
{% endfor %}
{% endfor %}
答案 0 :(得分:1)
假设我上面的评估是正确的:为什么不这样做呢?
我们使用经典for循环而不是使用foreach循环,并使用索引作为surveyQuestions
和{% for i in 0..(forms|length - 1)%}
<div class="overlay-container tab-content">
<div role="tabpanel" class="show-rendered-form tab-pane in fade active" id="show-rendered-form">
{% form_theme form with ['bootstrap_3_horizontal_layout.html.twig'] %}
{{ form(forms[i]) }}
<div class="overlay-mask">
<div class="btn-group">
<a class="edit-action btn btn-default" href="#show-edit-form"
data-question-id="{{ surveyQuestions[i].id }}" aria-controls="show-rendered-form"
role="tab" data-toggle="tab">Editeaza</a>
<a class="delete-action btn btn-default" href="#show-rendered-form"
data-question-id="{{ surveyQuestions[i].id }}" aria-controls="" role="tab"
data-toggle="tab">Sterge</a>
</div>
</div>
</div>
<div role="tabpanel" class="show-edit-form tab-pane" id="show-edit-form">
</div>
</div>
{% endfor %}
的标识符:
def test_yield(n):
for i in range(n):
yield str(i), str(i+1)
print '\n'.join([elem for seg in test_yield(5) for elem in seg])