DrupalX

Extension
Framework

Cache statement

Interestingly, by default, there is no way a Twig template can inform Drupal about its caching requirements. Obviously, there are some templates (e.g. for a news box paragraph etc.) that you'd like to refresh quite often - and other templates that you don't need to refresh that much. Some templates might need to be refresh if some other node is changing, others might need to change depending on whether a specific cookie is set or not. Drupal's sophisticated caching system fortunately supports all those use cases, but, as stated above, there is no way to make use of that easily from Twig.

With DrupalX, you can now use the new "cache" statement in Twig to define certain caching requirements. 

maxage

By using cache maxage you can define the maximum age as a caching dependency:

{% cache maxage(60) %}
Current time: {{ "now" | date("H:i:s") }}

In this simple example, you'll find that the template is refreshed every 60 seconds. If you refresh the page more often in your browser, the displayed seconds won't change. Please note, however, that you don't have all caches disabled anyway for development (development.services.yml) for this to work. If you use "cache maxage(0)", it tells Drupal to refresh that template for every page call.

Ok, fine, but there's one caveat, though: If you are using such a cache statement e.g. for a paragraph template, by default Drupal doesn't bubble this information up to the whole page. What does that mean? Well, your paragraph might get a refresh quite happily every 60 seconds if it was accessed, but: Drupal has decided to cache the whole page much longer, so your different paragraph settings are never considered. The underlying bug is discussed in Drupal issue 235009. You'll find patches there which are currently required for this DrupalX feature to work properly. We hope that this won't be necessary in the future anymore.

node

Another possibility is to make the caching dependent on changes of another node:

{%
set node=Node::query({title:"Home"});
cache node(node);
%}

{{node.$body}}

In this example, we search a node with the title "Home" and print its body field. The cache statement is added to declare that changes for this template depend on changes of the other node. Or, put differently, this template should be refreshed whenever the home page changes.

cookie

It can sometimes be very useful to let the caching depend on the existence of a cookie. For example, let's imagine you've added a special welcome box that will appear if someone visits the website. Lest vistors get annoyed by this welcome box, you are setting a cookie and if someone visits your site for a second time, you won't show that box. As soon as you enable Drupal's caching mechanism by disabling any development settings, you will find that your idea won't work: Drupal will cache the page with the welcome box and show that cached page even when the cookie is set.

By simply adding the following statement to your template (in this example the cookie's name is "welcome_box_shown") you can alleviate that problem with DrupalX:

{% cache cookie("welcome_screen_shown") %}