0

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?

1
  • 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.
    – sonfd
    Jan 14 at 14:39
1

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.

5
  • If I try {% if is_front %} I get the error Twig\Error\SyntaxError: Unknown "is_front" tag. in Twig\Parser->subparse()
    – stkuser
    4 hours ago
  • I've added the line $vars['is_front'] = \Drupal::service('path.matcher')->isFrontPage(); to my themename_preprocess_page() function and now it works
    – stkuser
    4 hours ago
  • Strange, this variable is already in core. See template_preprocess_page
    – 4k4
    3 hours ago
  • Do you mean in version 9.x? We use 8.x, I just specified it as a tag of the question, sorry
    – stkuser
    3 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.