【问题标题】:Add body class to node in Drupal 8在 Drupal 8 中将 body 类添加到节点
【发布时间】:2015-12-07 23:15:50
【问题描述】:

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

【问题讨论】:

    标签: twig drupal-8


    【解决方案1】:

    为body标签添加首页、登录和节点类型类。编辑 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) }}>
    

    【讨论】:

      【解决方案2】:

      您需要将这些代码添加到“html.html.twig”模板中,您可以在 drupal 8 的核心主题“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,
      
        ]
      %}
      ?>
      

      【讨论】:

        猜你喜欢
        • 2020-03-24
        • 1970-01-01
        • 2013-01-05
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多