如何检查当前页面是否是node.html.twig 的首页?

GIJ*_*IJO 2 variables templates frontpage twig drupal-8

我目前正在尝试将网站与 Drupal 8 集成。我的首页与其他页面略有不同,我需要检查 node.html.twig 当前页面是否为首页以添加“div” 。变量“is_front”在 page.html.twig 上工作正常,但在 node.html.twig 上似乎不可用。我该如何解决这个问题?

Cyc*_*ode 6

您还可以使用预处理挂钩添加此变量。以下代码将添加is_front变量,以便可以在模板中使用它html.html.twig

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

然后在树枝模板中,您可以像这样检查变量:

{% if is_front %}
THIS IS THE FRONTPAGE
{% endif %}
Run Code Online (Sandbox Code Playgroud)