Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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

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 我想在两次循环中显示一个div类(Symfony)_Loops_Symfony_If Statement_Bootstrap 4 - Fatal编程技术网

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=0
1%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 %}