Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ErnoVanhala on
I have put main navigation to two different areas. In area A the blocks shows only level 1 menu items and on area B it shows from level 2 to N. Now I would like to use different twig template for these two blocks but it seems that they both just use the same template. Is there any way to make two separate templates or even identify these two blocks so that I could make some if-else to the template file?
Comments
\,,/
Turn on twig debugging, the template suggestion names are output in html comments:
https://www.drupal.org/node/1903374
Note that the variable
{{ attributes.id }}
is unique.Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
I am using twig debugging and
I am using twig debugging and it gives both blocks the same template suggestions.
In menu--main.html.twig
attributes
variable exists, butattributes.id
is NULL.\"/
menu--main.html.twig
is not a block template, it's a menu template suggestion.Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
:)
Yes, that's true. My mistake, I am still quite new to twig.
So in
block--system-menu-block--main.html.twig
there isattributes.id
. Is there way to make one block to usemenu--main.html.twig
and second to usemenu--main--part2.html.twig
?I'll have to think about it,
I'll have to think about it, but I am short on time... however to dictate or provide template suggestions you use
hook_theme_suggestions_HOOK_alter()
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
This is exactly what I am trying to do as well
I am printing the menu twice in page twig, and need to use two different main menu templates...
Me too
I've got the same problem. Any ideas by now?
Found a solution here: http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-su...
Very nice article. This
Very nice article. This helped me too.