我想在标题区域的drupal模板中添加自定义类,但是它不起作用。
<!-- BEGIN OUTPUT from 'themes/marinenat/templates/layout/page.html.twig' -->
<div class="layout-container">
<header role="banner">
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'region' -->
<!-- FILE NAME SUGGESTIONS:
x region--header.html.twig
* region.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/marinenat/templates/layout/region--header.html.twig' -->
<div class="nav">
我想在“导航”类di区域(标题)之后添加此类。
任何人都可以帮助我
您已经在输出中拥有了所需的一切。
如果themes/marineat
您是以主题为基础,则只需将其复制themes/marinenat/templates/layout/region--header.html.twig
到子主题的themes/MYSUBTHEME/templates
目录中并编辑此文件。刷新缓存。做完了
如果themes/marinenat
是自定义子主题,则只需编辑建议的模板文件/themes/marinenat/templates/layout/region--header.html.twig/
并在其中添加类。刷新缓存。做完了
最后但并非最不重要的一点是,您还可以通过MYSUBTHEME.theme
文件或任何MYMODULE.module
文件的预处理功能将类添加到区域。
/**
* Implements template_preprocess_region().
*/
function MYTHEME/MYMODULE_preprocess_region(&$variables) {
if (isset($variables['region']) && $variables['region'] == 'header') {
$variables['attributes']['class'][] = 'MYCLASS';
}
}
/**
* Implements template_preprocess_region().
*/
function MYTHEME/MYMODULE_preprocess_region(&$variables) {
$variables['foo'] = FALSE;
if (isset($variables['region']) && $variables['region'] == 'header') {
// Get the current node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// Get the node type.
$node_type = $node->bundle();
// Do what ever else you need to do to retrieve your class dynamically.
$variables['foo'] = $node_type;
}
}
}
然后在您的region--header.html.twig
Twig文件中是:
{% if foo %}
<div class="nav {{ foo }}">
{{ content }}
</div>
{% endif %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句