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

Jeff Burnz’s picture

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.

ErnoVanhala’s picture

I am using twig debugging and it gives both blocks the same template suggestions.

In menu--main.html.twig attributes variable exists, but attributes.id is NULL.

Jeff Burnz’s picture

menu--main.html.twig is not a block template, it's a menu template suggestion.

ErnoVanhala’s picture

Yes, that's true. My mistake, I am still quite new to twig.

So in block--system-menu-block--main.html.twig there is attributes.id. Is there way to make one block to use menu--main.html.twig and second to use menu--main--part2.html.twig?

Jeff Burnz’s picture

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()

tkruger’s picture

I am printing the menu twice in page twig, and need to use two different main menu templates...

cake_cgn’s picture

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...

ab.shakir’s picture

Very nice article. This helped me too.