将类添加到“body”中

发布于 2024-12-08 14:52:16 字数 70 浏览 0 评论 0原文

如何修改或预处理 标记以添加类主体?我不想创建整个 html.tpl.php 只是为了添加一个类。

How can I modify or pre-process the <body> tag to add the class body? I don't want to create a whole html.tpl.php just to add a class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

内心激荡 2024-12-15 14:52:16

在主题的 template.php 文件中使用 preprocess_html 钩子:

function mytheme_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'new-class';
}

记住在实现钩子后清除缓存,否则 Drupal 将不会拾取它。

In your theme's template.php file use the preprocess_html hook:

function mytheme_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'new-class';
}

Remember to clear the caches once you've implemented the hook or Drupal won't pick it up.

云柯 2024-12-15 14:52:16

html.tpl.php 模板的文档 记录了$classes 变量作为类的字符串,可用于通过 CSS 根据上下文设置样式。。如果您查看模板的代码,会发现此变量用于生成的 body 元素的类属性中:

<body class="<?php print $classes; ?>" <?php print $attributes;?>>

$classes 变量实际上已由 template_process() 适用于任何模板文件,并根据 $classes_array 变量。

因此,要将类添加到页面正文中,您应该将该类添加到主题(或模块)的 hook_preprocess_html() 实现中的 $classes_array 值中:

function THEME_preprocess_html(&$variables) {
  $variables['classes_array'][] = 'new-class';
}

由于这是核心定义的模板和处理函数,任何表现良好的主题都应该重复使用相同的变量。

The documentation for the html.tpl.php template documents the $classes variables as String of classes that can be used to style contextually through CSS.. If you look at the code for the template, this variable is used in the class attributes of the produced body element:

<body class="<?php print $classes; ?>" <?php print $attributes;?>>

The $classes variables is actually already set by template_process() for any template file and build from the content of the $classes_array variable.

So to add a class to the body of your page, you should add this class to the $classes_array value from your theme (or module)'s implementation of hook_preprocess_html():

function THEME_preprocess_html(&$variables) {
  $variables['classes_array'][] = 'new-class';
}

Since this is the core defined template and process function, any well-behaving theme should re-use the same variables.

燕归巢 2024-12-15 14:52:16

我必须在同一个钩子中使用不同的数组键才能使其工作:

function THEME_preprocess_html(&$vars) {
  $vars['attributes_array']['class'][] = 'foo2';
}

I had to use different array keys in the same hook to make it work:

function THEME_preprocess_html(&$vars) {
  $vars['attributes_array']['class'][] = 'foo2';
}
蛮可爱 2024-12-15 14:52:16

Context 模块还允许您向 body 标记添加类。

如果您需要在某些条件下添加类,这可能很有用。

您可以在“主题 HTML”反应下找到此选项:

上下文 UI 中的主题 HTML 选项

The Context module allows you to add a class to the body tag as well.

This can be useful if you need the class to be added under certain conditions.

You find this options under the reaction "Theme HTML" :

Theme HTML option in Context UI

—━☆沉默づ 2024-12-15 14:52:16

Common Body Class模块提供用户通过接口向任意页面添加类。
该界面具有选择多个用户角色以及可以呈现类的页面的选项。

示例

Common Body Class module provide users to add classes to any page through the an interface.
The interface has options to select multiple user roles as well as pages where the class can be rendered.

Example

幻梦 2024-12-15 14:52:16

答案似乎取决于上下文。以下是我通过反复试验发现的结果:

如果您的 hook_preprocess_html() 位于模块中,请使用 $vars['classes_array'][]。

如果它在主题中,请使用$vars['attributes_array']['class'][]。

The answer appears to depend on context. Here's what I've found via trial-and-error:

If your hook_preprocess_html() is in a module, use $vars['classes_array'][].

If it's in a theme, use $vars['attributes_array']['class'][].

栖竹 2024-12-15 14:52:16

我在别人建立的网站上应用了这种技术。一开始它不起作用,但后来更深入地挖掘,发现 $classes 变量没有在 tpl 文件中输出。因此,如果它不起作用,请检查一下。

I applied this technique on a site that someone else built. It didn't work at first but then dug deeper and found that the $classes variable was not being output in the tpl file. So if it's not working, check that.

止于盛夏 2024-12-15 14:52:16

对于 Drupal 7,请安装 http://drupal.org/project/body_class。它将帮助您为 body 标记中的每个节点添加单独的类

For Drupal 7 install http://drupal.org/project/body_class. It will help you to add seperate classes for each node in body tag

习ぎ惯性依靠 2024-12-15 14:52:16

您可以检查“https://www.drupal.org/project/page_specific_class”来添加任何页面的 class 到 body 标签

You can check "https://www.drupal.org/project/page_specific_class" to add class to body tag of any page

梦罢 2024-12-15 14:52:16

这是一种基于 URL 添加类的简单方法,Drupal 9。
无需启用模块。

/**
 * Implements hook_preprocess_html().
 */
function THEME_NAME_preprocess_html(&$variables) {
  // Get the current path
  $current_path = \Drupal::service('path.current')->getPath();
  $internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

  // Assign it to body class 
  $variables['attributes']['class'][] = str_replace("/", "", $internal_path);
}

请参阅: http://www.thirstysix.com/how-can-i-add-body-class-based-path-page-specific-class-drupal-9

It's a simple way to add a class based on the URL, Drupal 9.
No need to enable the Modules.

/**
 * Implements hook_preprocess_html().
 */
function THEME_NAME_preprocess_html(&$variables) {
  // Get the current path
  $current_path = \Drupal::service('path.current')->getPath();
  $internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

  // Assign it to body class 
  $variables['attributes']['class'][] = str_replace("/", "", $internal_path);
}

Refer: http://www.thirstysix.com/how-can-i-add-body-class-based-path-page-specific-class-drupal-9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文