SF / Twig-计数具有相同值的属性 编辑

问题描述

我只想在树枝视图上显示一个属性值,其值定义为

 <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”

enter image description here

解决方法

尝试一下:

<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 %}

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

通常,将尽可能少的复杂计算放在树枝模板中是一个好主意。控制器和服务是一个更好的选择。

编辑

要回答您的评论,如果有几种状态,您可以创建一个变量,以票证的状态对其进行分组:

// In your controller:

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

// 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,],'En attente' => [
        [ 'id' => 2,'Ouvert' => [
        [ 'id' => 3,];

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 %}

由您决定使其适合您自己的需求。