【问题标题】:How to create a counter with TWIG?如何用 TWIG 创建一个计数器?
【发布时间】:2020-10-09 01:46:36
【问题描述】:

我有一个使用 Drupal 8 的站点,我想使用 TWIG 创建一个任务计数器。

我使用带有条件的视图。无论视图是否有结果,计数器都必须递增。

这是我刚刚编写的代码:

<span class="badge badge-warning task-badge-warning">
  {% if drupal_view_result('boutique_page_liste_des_taches_produit_non_publie', 'block_1') is not empty %}
    1
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_role_marchand', 'block_1') is empty %}
    1
  {% endif %}
</span>
<span class="badge badge-danger task-badge-danger">
  {% if drupal_view_result('boutique_page_liste_des_taches_aucun_produit', 'block_1') is empty %}
    1
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_aucune_variation', 'block_1') is not empty %}
    1
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_commande', 'block_1') is not empty %}
    1
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_mode_de_livraison', 'block_1') is empty %}
    1
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_passerelle_de_paiement', 'block_1') is empty %}
    1
  {% endif %}
</span>

有 2 个计数器:

  • “警告”徽章
  • “危险”徽章

你知道解决办法吗?

  • “警告”徽章必须显示“警告”任务的总数。
  • “危险”徽章必须显示“危险”任务的总数。

【问题讨论】:

  • 给定代码有什么问题?你有什么努力让它发挥作用?

标签: symfony twig operators drupal-8 drupal-views


【解决方案1】:

您可以set 变量然后递增它们:

{% set warnings = 0 %}
<span class="badge badge-warning task-badge-warning">
  {% if drupal_view_result('boutique_page_liste_des_taches_produit_non_publie', 'block_1') is not empty %}
    {% set warnings = warnings + 1 %}
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_role_marchand', 'block_1') is empty %}
    {% set warnings = warnings + 1 %}
  {% endif %}
  {{ warnings }}
</span>
{% set dangers = 0 %}
<span class="badge badge-danger task-badge-danger">
  {% if drupal_view_result('boutique_page_liste_des_taches_aucun_produit', 'block_1') is empty %}
    {% set dangers = dangers + 1 %}
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_aucune_variation', 'block_1') is not empty %}
    {% set dangers = dangers + 1 %}
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_commande', 'block_1') is not empty %}
    {% set dangers = dangers + 1 %}
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_mode_de_livraison', 'block_1') is empty %}
    {% set dangers = dangers + 1 %}
  {% endif %}
  {% if drupal_view_result('boutique_page_liste_des_taches_passerelle_de_paiement', 'block_1') is empty %}
    {% set dangers = dangers + 1 %}
  {% endif %}
  {{ dangers }}
</span>

【讨论】:

  • 谢谢,它有效。但只是一个小问题。为了更新计数器,我必须清空 Drupal 缓存。是否可以不缓存计数器?谢谢
  • 我猜想缓存的不是你的计数器值,而是视图结果。
  • 我为我的缓存问题打开了一个新问题。但是,我已禁用视图中的缓存,但这并不能解决问题:-( stackoverflow.com/questions/62460402/…
  • 我们可以使用JS更新计数器以避免缓存吗?
  • 我发现docs.craftcms.com/v3/fr/dev/tags/cache.html 并更新了我的代码pastebin.com/zc2kBpYY 但在Drupal 中出现错误“网站遇到意外错误。请稍后再试。”我的日志 "wig\Error\SyntaxError : Unknown "cache" tag.dans Twig\Parser->subparse()"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2019-11-24
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
相关资源
最近更新 更多