Good day. There is a problem, I have an array with tasks. It contains tasks that relate directly to me (for my performance), as well as tasks that I ordered.

I need to calculate the number of tasks that I have to complete, and the number of tasks that I have to complete.

This is the for loop for my tasks:

{% for task in tasks if (task.deadline > "now"|date(datetime_type)) and (task.perfomer.id == app.user.id) %} вывод задания {% endfor %} 

It looks like a loop for my orders:

 {% for task in tasks if (task.deadline > "now"|date(datetime_type)) and (task.mentor.id == app.user.id) and task.perfomer.id != app.user.id %} вывод задания {% endfor %} 

And before each loop I gently calculate how many tasks are in each "array". those. something like: {{ (tasks if (task.deadline > "now"|date(datetime_type)) and (task.perfomer.id == app.user.id))|length }}

    2 answers 2

    Loops in twig contain the loop variable.

     {% for task in tasks if (task.deadline > "now"|date(datetime_type)) and (task.perfomer.id == app.user.id) %} {% if loop.first %}Количество: {{ loop.length }}{% endif %} вывод задания {% endfor %} 

    http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

    UPD:

    It seems to me that they should have been divided into two arrays beforehand. And it would be even better to transfer ready-made variables to the representation. Well, since the question is, here it is:

     {% set tasksIn, tasksOut = [], [] %} {% for task in tasks %} {% if (task.deadline > "now"|date(datetime_type)) and (task.perfomer.id == app.user.id) %} {% set tasksIn = tasksIn|merge([task]) %} {% endif %} {% if (task.deadline > "now"|date(datetime_type)) and (task.mentor.id == app.user.id) and task.perfomer.id != app.user.id %} {% set tasksOut = tasksOut|merge([task]) %} {% endif %} {% endfor %} Назначенные мне: {{ tasksIn|length }} {% for task in tasksIn %}Вывод{% endfor %} Назначенные мной: {{ tasksOut|length }} {% for task in tasksOut %}Вывод{% endfor %} 
    • This is not quite the case; the number of tasks is not calculated here. Here is the result of the execution: array: 4 [▼ "parent" => array: 4 [▶] "index0" => 0 "index" => 1 "first" => true]. I need to count exactly the number of tasks, but before this loop. - Vladislav
    • @ Vladislav Added - check1st
    • Thank you, I think it’s better to divide them in advance and give them to the performance. I just wanted to avoid the repeating loop. Thanks for the answer. Twig has recently begun to learn, he thought he would have a good deal on everything :) - Vladislav

    In the accepted answer, twig is used as a programming language for building arrays. Not quite a suitable task for the template engine. Ideally, of course, the assembly of arrays is better to transfer to php. But if this is not possible, then another solution can be applied without creating new arrays.

    To do this, use set / endset :

     {% set tasksIn, tasksOut = 0, 0 %} {% set tasksIn_content %} {% for task in tasks if (task.deadline > "now"|date(datetime_type)) and (task.perfomer.id == app.user.id) %} {% set tasksIn = loop.index %} вывод задания {% endfor %} {% endset %} {% set tasksOut_content %} {% for task in tasks if (task.deadline > "now"|date(datetime_type)) and (task.mentor.id == app.user.id) and task.perfomer.id != app.user.id %} {% set tasksOut = loop.index %} вывод задания {% endfor %} {% endset %} Количество: {{ tasksIn }}, {{ tasksOut }} Назначенные мне: {{ tasksIn_content }} Назначенные мной: {{ tasksOut_content }} 

    That is, your cycle itself remains unchanged, only the wrapper and the assignment of a variable inside the loop are added.

    This approach is more universal, since it can also be used if you need to count several counters inside a cycle (for example, how many tasks were completed, how many took more than 5 hours) and display these counters BEFORE the list of tasks themselves.

    • Yes, that's exactly what I did at first. But then it turned out that I first had to count the number, and then start the loop. In fact, the easiest way to do this is as you write, and by means of JS later add the result to the proper place. - Vladislav