【问题标题】:Sf/Twig - count number of property with same valueSf/Twig - 计算具有相同值的属性的数量
【发布时间】:2020-08-14 21:21:57
【问题描述】:

我只想在树枝视图上显示属性的编号,其值定义如下

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

【问题讨论】:

  • 您好,您发送的图片是您的结果还是您的期望?你的问题到底是什么,因为不清楚。
  • 嗨,结果是肯定的,但我只想显示“2 en attente”。就这样。我不知道是否需要使用特定的过滤器或使用变量
  • 所以你想显示状态为“En attente”的票的总数。没有你在代码中显示的 div ?
  • 具体工单的总和下方列表
  • 请通过编辑来澄清您的问题。 “计数”是什么意思?

标签: symfony twig


【解决方案1】:

如果我必须实现这样的事情,我会在 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, '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 %}

这取决于您自己的需要。

【讨论】:

  • 确实,在控制器中实现的更好方法。谢谢!
  • 我可以在函数中使用很多参数吗,就像我会使用很多过滤器 "En attente","Ouvert",.. ?
【解决方案2】:

试试这个:

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

【讨论】:

  • 结果一样,要么我们设置一个计数变量,要么设置一个循环变量。
  • 不,不是!!因为 loop.index 将为您提供它正在执行的循环数,无论它是否通过您的if。因此,如果您的票状态将是 2 张票之间的“Terminé”,那么您将有 1 个 En attente,3 个 En attente。因为第二个将是另一个法规。因为计数会真实显示票数!
猜你喜欢
  • 2022-07-03
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
  • 2015-10-26
相关资源
最近更新 更多