How to handle twig view and bootstrap 3 rows / columns?

I have an Article object and I created findAll () in the controller.

I displayed each article in a div with class col-md-6 .

But for two articles, I have to wrap these divs in a div string.

How to do this with a twig?

Thanks.

EDIT:

I tried your code (NHG) as follows:

{% for article in articles %}
    {% if loop.index % 2 == 0 %}
        <div class="row"></div>
    {% endif %}
    <div class="col-md-6">
        <article class="well well-sm">
            <a href="#"><img src="{{ article.image }}" alt="{{ article.title }}" class="img-thumbnail"></a>
            <h2 class="h3 text-center"><a href="#">{{ article.title }}</a></h2>
            <div class="alert alert-success well-sm">
                {{ article.content|striptags|slice(0, 235) }}...
            </div>
            <a class="btn btn-default btn-sm pull-right" href="#">{{ article.comments|length }} Comments</a>
            <div class="btn-group btn-group-sm">
                {% for tag in article.tags %}
                    <a class="btn btn-default">{{ tag.name }}</a>
                {% endfor %}
            </div>
        </article>
    </div>
{% endfor %}

But that will not work.

I want to have something like this:

<div class="row">
    <div class="col-md-6"></div>
    <div class="col-md-6"></div>
</div>

<div class="row">
    <div class="col-md-6"></div>
    <div class="col-md-6"></div>
</div>

<div class="row">
    <div class="col-md-6"></div>
    <div class="col-md-6"></div>
</div>
+4
source share
3 answers

UPDATED

As @Maerlyn suggested:

{% for row in articles|batch(2) %}
    <div class="row">
        {% for article in row %}
            <div class="col-md-6">
                // your content
            </div>
        {% endfor %}
    </div>
{% endfor %}

Old way:

loop.index (doc: ), modulo (doc: ) if (doc: if statement)

{% for article in articles %}
    {% if loop.index % 2 == 1 %}
        <div class="row">
    {% endif %}
    <div class="col-md-6">
        // your content
    </div>
    {% if (loop.index % 2 == 0 or loop.last) %}
        </div>
    {% endif %}
{% endfor %}
+19
{% for row in articles|batch(2) %}
    <div class="row">
        {% for article in row %}
            <div class="col-md-6">
                // your content
             {% if loop.index is divisibleby(3) %}
                 </div>
             {% endif %}
        {% endfor %}
    </div>
{% endfor %}
+2

I suggest:

<div class="row">
{% for article in articles %}
{% if (loop.index0 % 2 == 0) and not loop.first %}
</div>
<div class="row">
{% endif %}
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
    // your content
    </div>
{% endfor %}
</div>
+2
source

Source: https://habr.com/ru/post/1524921/


All Articles