2

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?

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
GIJO
  • 47
  • 1
  • 1
  • 7
  • A node is *content*. The front page is a *page*. If you are on a node, you can't be at the same time on the front page. Can you explain further what exactly you are trying to do? Maybe I don't understand correctly. – Frank Drebin Apr 28 '16 at 11:51

2 Answers2

6

You can also add this variable by using a preprocess hook. The following code will add the is_front variable so it can be used in the html.html.twig template:

// Adds the is_front variable to html.html.twig template.
function mytheme_preprocess_html(&$variables) {
  $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}

Then inside the twig template you could check the variable like so:

{% if is_front %}
THIS IS THE FRONTPAGE
{% endif %}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
1

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
Frank Drebin
  • 1,063
  • 6
  • 11
  • Thanks a lot for your answer. How can I set the display mode I want to use in a specific situation ? – GIJO Apr 28 '16 at 14:20