18

Im trying to show content when I'm on the front page of my Drupal 8 test website, but the content shows on every page:

{% if front_page %}
  <header role="banner">
    {% if logo %}
      <a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home">
        <img src="{{ logo }}" alt="{{ 'Home'|t }}"/>
      </a>
    {% endif %}
    <h1 style="color:black">TESTTESTTESTESTAET</h1>
<h1>test</h1

    {{ page.header }}
  </header>
{% endif %}

Why doesn't this work and how can I make this work?

0

5 Answers 5

31

The variable you're looking for is called is_front:

{% if is_front %}

The available variables for a template are documented at the top of the .html.twig file, however there is also a set of default variables available to all templates (which are not documented in every single template). You can find them in _template_preprocess_default_variables().

2
25

Note that if you want to provide $variables['is_front'] to templates that doesn't, you can add this into the concerned preprocess function.

function themename_preprocess_menu(&$variables) {
  try {
    $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
  }
  catch (Exception $e) {
    $variables['is_front'] = FALSE;
  }
}

It is done the same way in template_preprocess_page for page.html.twig.

Same answer here

3
  • 1
    This was useful for me in template_preprocess_html, as "is_front" is not available by default in html.html.twig Jan 3, 2017 at 20:00
  • I think you ought to add url.path.is_front as a cache context as well. See this comment.
    – Andy
    Aug 5, 2018 at 9:15
  • It doesn't work in TWIG for a block. How do I do it in a block?
    – mevsme
    Nov 3, 2022 at 18:19
4

Assuming that the template you are using is page.html.twig, then the variable you are looking for is is_front; front_page contains the URL of the front page, initialized in template_preprocess_page() with the following code.

$variables['front_page'] = \Drupal::url('<front>');

For other template files, Drupal doesn't provide any variables telling you if the user is visiting the front page. Using code similar to the one used from template_preprocess_page(), you can set that variable in the preprocess function for the template you are using.

function mymodule_preprocess_HOOK(&$variables) {
  // An exception might be thrown.
  try {
    $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
  }
  catch (Exception $e) {
    // If the database is not yet available, set the default value.
    $variables['is_front'] = FALSE;
  }  
}

Replace HOOK with the template type (e.g. field for the field.hmtl.twig template).

4

You can get it directly in html.html.twig using root_path. It is already output on line 31 to set the body class:

{% if not root_path %}

CODE

{% endif %}

1

for field template this is the code that needs to be placed in mytheme.theme :

        function mytheme_preprocess_field(&$vars, $hook) {
          $element = &$vars['element'];
          if ($element['#field_name'] == 'field_page_paragraphs') {
            $vars['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
          }
        }

Your Answer

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

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