Loops 我想在两次循环中显示一个div类(Symfony)
我正在做一个个人项目,我想这样做: 我的代码中有一个循环,运行良好:Loops 我想在两次循环中显示一个div类(Symfony),loops,symfony,if-statement,bootstrap-4,Loops,Symfony,If Statement,Bootstrap 4,我正在做一个个人项目,我想这样做: 我的代码中有一个循环,运行良好: {% for uneInfo in lesInfos %} <div class="row featurette "> <div class="col-md-7 {% if (loop.first or loop.last) %} order-md-0 {% else %} order-md-2 {% endif %}"> &
{% for uneInfo in lesInfos %}
<div class="row featurette ">
<div class="col-md-7 {% if (loop.first or loop.last) %} order-md-0 {% else %} order-md-2 {% endif %}">
<h2 class="featurette-heading"> {{ uneInfo.title }} </h2>
<p class="lead">{{ uneInfo.description }}</p>
</div>
<div class="col-md-5 {% if (loop.first or loop.last) %} order-md-0 {% else %} order-md-1 {% endif %} ">
<img class="featurette-image img-fluid mx-auto" data-src="holder.js/500x500/auto"
alt="Generic placeholder image">
</div>
</div>
<hr class="featurette-divider">
{% endfor %}
{%for lesInfos%中的uneInfo}
{{uneInfo.title}}
{{uneInfo.description}
{%endfor%}
在我的页面上,它看起来像这样:
如何在循环中交替显示?我和Symfony一起工作。你可以使用一个2作为模的操作符来决定你是在一个偶数循环索引中还是在一个不均匀循环索引中
{# if loop.index % 2 == 0 #}
0%2=01%2=1
2%2=0
3%2=1
4%2=0
5%2=1
等等。另一个机会
{% for i in 1..10 if i is not odd %}
{{ i }},
{% endfor %}
或
或
只使用代码的答案几乎总是可以通过添加一些关于如何和为什么的解释来改进的。
{% for i in range(low=2, high=10, step=2) %}
{{ i }},
{% endfor %}
{% for i in 1..10 %}
{% if loop.index is not odd %}
{{ i }},
{% endif %}
{% endfor %}