Saadia Saadia - 2 months ago 31
PHP Question

twig break out of loop and then continue the rest in next div

I am trying to loop through an array of meeting objects each meeting on same date needs to be displayed inside its own div and if the date changes then that date meeting needs to be in next div so the output i am looking for is something like this

<div class="row">
<h2 class="date-heading">September 24</h2>
<div class="col4">Meeting 1 </div>
<div class="col4">Meeting 2 </div>
<div class="col4">Meeting 3 </div>
<div class="col4">Meeting 4 </div>
<div class="col4">Meeting 5 </div>
</div>

<div class="row">
<h2 class="date-heading">September 25</h2>
<div class="col4">Meeting 1 </div>
<div class="col4">Meeting 2 </div>
<div class="col4">Meeting 3 </div>
<div class="col4">Meeting 4 </div>
<div class="col4">Meeting 5 </div>
</div>

<div class="row">
<h2 class="date-heading">September 26</h2>
<div class="col4">Meeting 1 </div>
<div class="col4">Meeting 2 </div>
<div class="col4">Meeting 3 </div>
<div class="col4">Meeting 4 </div>
<div class="col4">Meeting 5 </div>
</div>


Now inside the loop i have access to a parameter of each object called
request.isNewDate
, this is set to
yes
as soon as the current loop has a different date than the previous loop, i think i can use this to achieve what i need but i am having a problem in the logic.

This is what my code looks like where I am looping, how can i break out of this loop if the
request.isNewDate
is
yes
and then start the rest inside a new div.

Any help will be appreciated.

<div class="row">
{% for request in pagination %}
<div class="col4">

<h5 class="">{{ request.getGuest.getFirstName }}</h5>
<h6 class="text-transform-none">{{ request.getGuest.getCollege }}<br>
{{ request.getGuest.getDegree }}<br>
({{ request.getGuest.getCourse }})
</h6>
</div>
</div>

Answer

Try this snipped code:

{% for request in pagination %}

    {# 
        If is a new date, open a new div TAG and dump the data.
        If the first element of the collection do not have this info
        consider to use loop.index0 as OR operation.
    #}
    {% if request.isNewDate %}
    <div class="row">
                                  {# print and format the date #}
        <h2 class="date-heading">{{ request.date }}</h2>
    {% endif %}

    {# Render data of an element, consider to wrap this data in a single twig and incude #}
    <div class="col4">

        <h5 class="">{{ request.getGuest.getFirstName }}</h5>
        <h6 class="text-transform-none">{{ request.getGuest.getCollege }}<br>
            {{ request.getGuest.getDegree }}<br>
            ({{ request.getGuest.getCourse }})
        </h6>
    </div>

    {# remember to close the div #}
    {% if request.isNewDate %}
        </div>
    {% endif %}


{% endfor %}

Hope this help