Twig 将body类添加到Drupal 8中的节点

Twig 将body类添加到Drupal 8中的节点,twig,drupal-8,Twig,Drupal 8,我正在试验Drupal8。我已经创建了一个本地沙箱站点,并使用Twig(page--front.html.Twig)创建了一个自定义主题和一个自定义首页。我想添加一些只针对首页的定制CSS,我习惯于针对body.page-node-x(Drupal 7)。在Drupal8中,我在向这个定制页面添加body类时遇到问题。我看到的唯一实体类是工具栏托盘打开工具栏固定工具栏水平 您需要在“html.html.twig”模板中添加这些代码,您可以在Drupal8的核心主题“classy”中找到它们 &l

我正在试验Drupal8。我已经创建了一个本地沙箱站点,并使用Twig(page--front.html.Twig)创建了一个自定义主题和一个自定义首页。我想添加一些只针对首页的定制CSS,我习惯于针对body.page-node-x(Drupal 7)。在Drupal8中,我在向这个定制页面添加body类时遇到问题。我看到的唯一实体类是工具栏托盘打开工具栏固定工具栏水平

您需要在“html.html.twig”模板中添加这些代码,您可以在Drupal8的核心主题“classy”中找到它们

<?php
{%
  set body_classes = [
    logged_in ? 'user-logged-in',
    not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
    node_type ? 'page-node-type-' ~ node_type|clean_class,

  ]
%}
?>

为body标签添加frontpage、logged in和node type类。编辑html.html.twig并添加:

{%
  set body_classes = [
    logged_in ? 'logged-in',
    not root_path ? 'frontpage' : 'path-' ~ root_path|clean_class,
    node_type ? 'node--type-' ~ node_type|clean_class,
  ]
%}

<body{{ attributes.addClass(body_classes) }}>
{%
设置正文\u类=[
“已登录”?“已登录”,
不是根路径?'frontpage':'path-'~root_path | clean_类,
node_type?'node--type-'~node_type | clean_类,
]
%}