Score:0

Middle part of a page disappears and appears again after flushing all caches

cn flag

Sometimes middle part of some page at our website may disappear and only header and footer are still displayed. The problem is always solved after flushing all caches.

Our html.html.twig loads templates a way like

{% include '@themename/header.html.twig' %}
{{ page }}
{% include '@themename/footer.html.twig' %}

And our page.html.twig loads an appropriate template depenging on the page path like

{% set path = path('<current>') %}
{% set pagePath = path|render|render %}
{% if pagePath == '/node' %} {# if homepage #}
{% include '@themename/homepage.html.twig' %}
{% else %}
{% include '@themename/innerpage.html.twig' %}
{% endif %}

We are not well familiar with Drupal caching so we don't know how exactly the problem is related to the caching. Maybe it's related somehow to render cache? As we use path|render|render in our page.html.twig. Or maybe it's related somehow to index.php which is appeared sometimes in the address bar?

sonfd avatar
in flag
There's already a `page--front.html.twig` template / suggestion you can use to target only the front page (whatever it's set to on the Basic Site Settings config page). Use that instead.
Score:1
cn flag

The page template lists under General Utility Variables the is_front variable:

{% if is_front %}
  {% include '@themename/homepage.html.twig' %}
{% else %}
  {% include '@themename/innerpage.html.twig' %}        
{% endif %}

This should solve the problem, even if you visit the page by different URLs. You can avoid that as well, the Redirect module redirects out-of-the-box different paths of a page to a canonical one. Then the path based condition will be predictable, so this would be a second way to solve the problem.

cn flag
If I try `{% if is_front %}` I get the error `Twig\Error\SyntaxError: Unknown "is_front" tag. in Twig\Parser->subparse()`
cn flag
I've added the line `$vars['is_front'] = \Drupal::service('path.matcher')->isFrontPage();` to my `themename_preprocess_page()` function and now it works
4uk4 avatar
cn flag
Strange, this variable is already in core. See [template_preprocess_page](https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/template_preprocess_page)
cn flag
Do you mean in version 9.x? We use 8.x, I just specified it as a tag of the question, sorry
4uk4 avatar
cn flag
Same for 8.x. See https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/template_preprocess_page/8.9.x
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.