首页 > 解决方案 > 如何让计数器 TWIG 忽略 Drupal 缓存?

问题描述

这个问题与我之前的问题有关:

如何用 TWIG 创建一个计数器?

我有一个带有 Drupal 8 的站点和一个包含任务计数器的菜单:

在此处输入图像描述

计数器工作正常,但要更新它,我每次都必须清空 drupal 缓存。

我不是在尝试实时更新计数器,而是在我重新加载页面时。

这是菜单代码:

商业商店--专业--menu.html.twig

<nav role="navigation" aria-labelledby="menu-page-boutique" id="menu-page-boutique" class="contextual-region">
  <ul class="nav navbar-nav m-0">
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fas fa-plus-circle fa-lg"></i> Gérer votre {{ store_entity.type.entity.label }}</a>
      <ul class="dropdown-menu">
        <li class="task">
          <a href="/store/{{ store_entity.id }}/tasks" data-drupal-link-system-path="/store/{{ store_entity.id }}/tasks">
            <i class="fas fa-tasks fa-lg"></i> <b>Liste des tâches</b>
            <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>
            <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>
          </a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/point-of-sale" data-drupal-link-system-path="/store/{{ store_entity.id }}/point-of-sale"><i class="fas fa-cash-register fa-lg"></i> Point de vente</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/migrate" data-drupal-link-system-path="/store/{{ store_entity.id }}/migrate"><i class="fas fa-exchange-alt  fa-lg"></i> Migrer</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}" data-drupal-link-system-path="/store/{{ store_entity.id }}"><i class="fas fa-glasses  fa-lg"></i> Voir</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/edit" data-drupal-link-system-path="/store/{{ store_entity.id }}/edit"><i class="fas fa-feather-alt fa-lg"></i> Modifier</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/delete" data-drupal-link-system-path="/store/{{ store_entity.id }}/delete"><i class="fas fa-trash-alt fa-lg"></i> Supprimer</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/translations" data-drupal-link-system-path="/store/{{ store_entity.id }}/translations"><i class="fas fa-globe fa-lg"></i> Traductions</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/members" data-drupal-link-system-path="/store/{{ store_entity.id }}/members"><i class="fas fa-sitemap fa-lg"></i> L'équipe</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/relationship" data-drupal-link-system-path="/store/{{ store_entity.id }}/relationship"><i class="fas fa-link  fa-lg"></i> Relations</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/products" data-drupal-link-system-path="/store/{{ store_entity.id }}/products"><i class="fas fa-gift  fa-lg"></i> Produits</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/promotions" data-drupal-link-system-path="/store/{{ store_entity.id }}/promotions"><i class="fas fa-percentage fa-lg"></i> Promotions</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/payment-gateways" data-drupal-link-system-path="/store/{{ store_entity.id }}/payment-gateways"><i class="fas fa-credit-card fa-lg"></i> Modes de paiement</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/shipping-methods" data-drupal-link-system-path="/store/{{ store_entity.id }}/shipping-methods"><i class="fas fa-truck fa-lg"></i> Modes de livraison</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/orders" data-drupal-link-system-path="/store/{{ store_entity.id }}/orders"><i class="fas fa-shopping-cart fa-lg"></i> Commandes</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/inventory" data-drupal-link-system-path="/store/{{ store_entity.id }}/inventory"><i class="fas fa-clipboard-list fa-lg"></i> Inventaire</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/reports" data-drupal-link-system-path="/store/{{ store_entity.id }}/reports"><i class="fas fa-calculator fa-lg"></i> Rapports de vente</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/like" data-drupal-link-system-path="/store/{{ store_entity.id }}/like"><i class="fas fa-heartbeat fa-lg"></i> Mentions j'aime</a>
        </li>
        <li>
          <a href="/store/{{ store_entity.id }}/seo" data-drupal-link-system-path="/store/{{ store_entity.id }}/seo"><i class="fas fa-flag-checkered fa-lg"></i> Référencement</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

菜单是一种显示模式(在 Commerce 中),它使用上面的 TWIG 文件进行修改,并使用视图块呈现。

所以我的问题。如何让计数器忽略 Drupal 缓存?

在此处输入图像描述

在此处输入图像描述

如果我们不能用 TWIG 更新,我们可以使用 JS 吗?

在我的主题中,我有一个 JS 文件:

引导程序.js

(function ($, Drupal) {

  'use strict';

})(jQuery, Drupal);

我应该输入什么来更新我的任务计数器?

标签: javascriptsymfonycachingtwigdrupal-8

解决方案


通常有两种解决方案可以解决 http 缓存问题,并且仍然将页面缓存保留在 Symfony 中。

第一个是加载不应通过 ajax 请求缓存的数据。

最简单的方法是使用hinclude

https://symfony.com/doc/current/templating/hinclude.html

否则,您可以使用ESI对未缓存的内容进行子请求:

https://symfony.com/doc/current/http_cache/esi.html

对于这两种情况,您都需要创建一个新的控制器来加载您想要呈现的数据。


推荐阅读