您还可以使用预处理挂钩添加此变量。以下代码将添加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)