将类添加到“body”中
如何修改或预处理 标记以添加类主体?我不想创建整个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在主题的
template.php
文件中使用preprocess_html
钩子:记住在实现钩子后清除缓存,否则 Drupal 将不会拾取它。
In your theme's
template.php
file use thepreprocess_html
hook:Remember to clear the caches once you've implemented the hook or Drupal won't pick it up.
html.tpl.php 模板的文档 记录了
$classes
变量作为类的字符串,可用于通过 CSS 根据上下文设置样式。。如果您查看模板的代码,会发现此变量用于生成的 body 元素的类属性中:$classes
变量实际上已由template_process()
适用于任何模板文件,并根据$classes_array
变量。因此,要将类添加到页面正文中,您应该将该类添加到主题(或模块)的
hook_preprocess_html()
实现中的$classes_array
值中:由于这是核心定义的模板和处理函数,任何表现良好的主题都应该重复使用相同的变量。
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:The
$classes
variables is actually already set bytemplate_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 ofhook_preprocess_html()
:Since this is the core defined template and process function, any well-behaving theme should re-use the same variables.
我必须在同一个钩子中使用不同的数组键才能使其工作:
I had to use different array keys in the same hook to make it work:
Context 模块还允许您向 body 标记添加类。
如果您需要在某些条件下添加类,这可能很有用。
您可以在“主题 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" :
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.
答案似乎取决于上下文。以下是我通过反复试验发现的结果:
如果您的 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'][].
我在别人建立的网站上应用了这种技术。一开始它不起作用,但后来更深入地挖掘,发现 $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.
对于 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
您可以检查“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
这是一种基于 URL 添加类的简单方法,Drupal 9。
无需启用模块。
请参阅: 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.
Refer: http://www.thirstysix.com/how-can-i-add-body-class-based-path-page-specific-class-drupal-9