Sf/Twig - 计算具有相同值的 属性 的个数

Sf/Twig - count number of property with same value

我只想在树枝视图上显示 属性 的数量,其值定义为

 <div class="list-group">
          {% for ticket in tickets %}
            {% if ticket.statut == 'En attente' %}
                {{ loop.index }}
                {{ ticket.statut }}
                <div class="d-flex justify-content-start"><a href="{{ path('ticket', {id:ticket.id}) }}" class="list-group-item list-group-item-action active"><i class="fas fa-ticket-alt"></i> {{ ticket.getNomProduit }}</div></a>
            {% endif %}
          {% endfor %}
          </div>

但我得到了正确的号码 (2),但我只会显示此特定票证的号码,其值为“En attente”

试试这个:

<div class="list-group">
         {% set count = 0 %}
          {% for ticket in tickets %}
            {% if ticket.statut == 'En attente' %}
                {{ loop.index }}
                {{ ticket.statut }}
                <div class="d-flex justify-content-start"><a href="{{ path('ticket', {id:ticket.id}) }}" class="list-group-item list-group-item-action active"><i class="fas fa-ticket-alt"></i> {{ ticket.getNomProduit }}</div></a>
               {% set count = count + 1 %}
            {% endif %}
          {% endfor %}
          {{ count }} Total File En attente
</div>

如果我必须实现这样的事情,我会在 PHP 控制器的某个地方做,像这样:

// In the controller:

$ticketsEnAttente = array_filter($tickets,
    function($ticket) {
        return $ticket['statut'] === 'En attente';
    }
);

return $this->render('my_template.html.twig', [
    'tickets_en_attente' => $ticketsEnAttente,
]);

然后在 Twig 中显示计数和不同的票是微不足道的,比如:

<div>
    {{ tickets_en_attente | length }} tickets en attente:
</div>
{% for ticket in tickets_en_attente %}
    {{ loop.index }}
    <div class="d-flex justify-content-start">
        <a href="{{ path('ticket', {id:ticket.id}) }}" class="list-group-item list-group-item-action active">
            <i class="fas fa-ticket-alt"></i>
            {{ ticket.getNomProduit }}
        </div>
    </a>  {# NOTE: there is an html error in your code, </div> and </a> are inverted #}
{% endfor %}

这里唯一的区别是循环索引将与您提供的原始代码不同,因为它只考虑过滤后的列表。

一般来说,在 twig 模板中放置尽可能少的复杂计算是个好主意。控制器和服务是一个更好的地方。

编辑

要回答您的评论,如果有多个状态,您可以创建一个变量,按状态对工单进行分组:

// In your controller:

// Mock data:
$tickets = [
    [ 'id' => 1, 'statut' => 'Fermé' ],
    [ 'id' => 2, 'statut' => 'En attente' ],
    [ 'id' => 3, 'statut' => 'Ouvert' ],
    [ 'id' => 4, 'statut' => 'En attente' ],
];

// Group tickets by status:
$ticketsByStatus = [];
foreach ($tickets as $ticket) {
    $ticketsByStatus[$ticket['statut']][] = $ticket;
}

// This is equivalent to writing this by hand:
$ticketsByStatus = [
    'Fermé' => [
        [ 'id' => 1, 'statut' => 'Fermé' ],
    ],
    'En attente' => [
        [ 'id' => 2, 'statut' => 'En attente' ],
        [ 'id' => 4, 'statut' => 'En attente' ],
    ],
    'Ouvert' => [
        [ 'id' => 3, 'statut' => 'Ouvert' ],
    ],
];

return $this->render('default/index.html.twig', [
    'tickets_by_status' => $ticketsByStatus,
]);

然后您可以遍历模板中的每个状态(如果您愿意,也可以手动访问它们),如下所示:

{% for status in tickets_by_status|keys %}
    <h4>{{ status }}:</h4>
    <ul>
        {% for ticket in tickets_by_status[status] %}
            <li>
                {{ ticket.id }}
                {#   {{ ticket.getNomProduit }}   #}
            </li>
        {% endfor %}
    </ul>
{% endfor %}

您可以根据自己的需要进行调整。