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
  • 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
    Commented Jan 14, 2022 at 14:39

1 Answer 1

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()
    – user106480
    Commented Jan 18, 2022 at 9:10
  • I've added the line $vars['is_front'] = \Drupal::service('path.matcher')->isFrontPage(); to my themename_preprocess_page() function and now it works
    – user106480
    Commented Jan 18, 2022 at 9:26
  • Strange, this variable is already in core. See template_preprocess_page
    – 4uk4
    Commented Jan 18, 2022 at 10:19
  • Do you mean in version 9.x? We use 8.x, I just specified it as a tag of the question, sorry
    – user106480
    Commented Jan 18, 2022 at 10:26
  • Same for 8.x. See api.drupal.org/api/drupal/core%21includes%21theme.inc/function/…
    – 4uk4
    Commented Jan 18, 2022 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.