As far as I know $is_front is a global template variable in Drupal 7 and therefore the variable is available in all templates.

Now for Drupal 8 it seems that this is no longer true. I tried to call for $is_front in page-title.html.twig and realized that $is_front was not available here.

Using template_preprocess_page_title(&$variables) I could add $is_front to the template.

*Why is Drupal 8 not providing $is_front in the same style as Drupal 7? And what would be the way to check for frontpage in a template where $is_front is not available?**

It also seems that drupal_is_front_page() is not available anymore.

Should I go like:

  try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();


      catch (Exception $e) {
        // If the database is not yet available, set default values for these
        // variables.
        $variables['is_front'] = FALSE;
        $variables['db_is_active'] = FALSE;

(taken from template_preprocess_page)

  • According to the page.html.twig documentation, the is_front variable is provided for page.html.twig in all the Drupal 8 core themes. Would it be enough for what you need?
    – dinopmi
    Commented Nov 25, 2015 at 14:02
  • Yes is_front is available in page.html.twig while this is the only template as far as I can see. So I am searching for a good way to get this variable in other templates too - just like it is in D7 Commented Nov 25, 2015 at 16:01

1 Answer 1


You almost answered the question yourself, you can add the variable to all templates with this code:

function theme_preprocess(&$variables, $hook) {
  try {
    $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
  catch (Exception $e) {
    $variables['is_front'] = FALSE;
  // Ensure the cache varies correctly (new in Drupal 8.3).
  $variables['#cache']['contexts'][] = 'url.path.is_front';

For performance reason you can make a condition on $hook to only add the variable to the templates you need, and to avoid that the code is run twice for page.


New in Drupal 8.3: Cache context for determining the Front Page

  • while this works in principle, there is some issue with caching if I am trying to access the variable in some templates for example block--system-branding-block.html.twig. I clear the cache go to frontpage and is_front is true. Then I go to a node and is_front still gives true, only if I clear the cache again, then is_front is false. Commented Nov 26, 2015 at 11:18
  • 3
    Drupal caches the branding block, until there are changes to the system.site in config. This is set as cache tag for this block. You can try to alter the block with $variables['#cache']['contexts'][] = 'url'. If this does not work, the most straightforward solution would be to use two different branding blocks and configure the visibility in the block layout depending on the url <front>. Then both versions are in the cache separately and there will be no performance loss by rebuilding the block on each different url.
    – 4uk4
    Commented Nov 26, 2015 at 13:05
  • Two branding blocks is great! Did not realize that it is that easy in D8 to have multiple blocks of the same. Thanks for your insights. Commented Nov 26, 2015 at 14:49

Your Answer

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

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