You already have all you need in the output.
Many roads lead to Rome
If themes/marineat
is you base theme, simply copy themes/marinenat/templates/layout/region--header.html.twig
into your subtheme's themes/MYSUBTHEME/templates
directory and edit this file. Flush cache. Done.
If themes/marinenat
is you custom subtheme simply edit the suggested template file /themes/marinenat/templates/layout/region--header.html.twig/
and add your class there. Flush cache. Done.
Last but not least you can also add classes to a region from a preprocess function from your MYSUBTHEME.theme
file or any MYMODULE.module
file.
/**
* Implements template_preprocess_region().
*/
function MYTHEME/MYMODULE_preprocess_region(&$variables) {
if (isset($variables['region']) && $variables['region'] == 'header') {
$variables['attributes']['class'][] = 'MYCLASS';
}
}
How to pass a string from PHP to Twig
/**
* 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;
}
}
}
Then in your region--header.html.twig
Twig file it's:
{% if foo %}
<div class="nav {{ foo }}">
{{ content }}
</div>
{% endif %}