如何使计数器TWIG忽略Drupal缓存? [英] How to make the counter TWIG ignore the Drupal cache?

查看:80
本文介绍了如何使计数器TWIG忽略Drupal缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This question is related to my previous question :

如何使用TWIG创建计数器?

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

I have a site with Drupal 8 and a menu which includes a task counter :

在此处输入图像描述

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

The counter works fine, but to update it, I have to empty the drupal cache every time.

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

I am not trying to update the counter in real time, but when I reload the page.

这是菜单代码:

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

commerce-store--professionnel--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文件进行了修改,

The menu is a display mode (in Commerce) which is modified using the TWIG file above and which is rendered with a view block.

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

So my question. How to make the counter ignore the Drupal cache ?

在此处输入图片描述

在此处输入图片描述

如果我们无法使用TWIG更新,可以使用JS吗?

If we can't update with TWIG, can we use JS?

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

In my theme I have a JS file :

bootstrap.js

bootstrap.js

(function ($, Drupal) {

  'use strict';

})(jQuery, Drupal);

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

推荐答案

主要有2种解决方案来解决http缓存并仍然将页面缓存保留在Symfony中。

There are mostly 2 solutions to workaround the http cache and still keep the page cache in Symfony.

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

First one is you load the data which should not be cached over an ajax request.

最简单的方法是使用 hinclude

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

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

Else you can use ESI which will do a sub request for the uncached content:

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

对于两者,您都需要创建一个新的控制器,该控制器将加载要呈现的数据。

For both you need to create a new controller which will load the data you want to be rendered.

这篇关于如何使计数器TWIG忽略Drupal缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆