templatesvariablestwigdrupal-8frontpage

How do I check if the current page is the front page from node.html.twig?


I'm currently trying to integrate a website with Drupal 8. My front page is just a bit different than the other pages and I need to check in node.html.twig if the current page is the front page to add a "div". The variable "is_front" is working fine on page.html.twig but it seems not to be available on node.html.twig. How could I fix this problem?


Solution

  • If you want to show a node within the front page and it should look just like the actual node page, you can create a new display for the node, like "On Frontpage". For that display you create a new node template (be careful to use the right naming convention for the twig file, otherwise it won't work). Then you tell the front page to display nodes using the "On Frontpage" display, which will use a different template (including your desired div).

    Twig templates naming convention: https://www.drupal.org/node/2354645

    So the steps:

    1. create a new display mode for your nodes
    2. create a new template for that particular display mode
    3. tell the frontpage to display nodes using that display