KnpMenu icon indicating copy to clipboard operation
KnpMenu copied to clipboard

Render menu containing only siblings of active menu item

Open bkosborne opened this issue 11 years ago • 5 comments

I'm curious, is there a way to render a partial menu containing siblings (and maybe children) of the current menu item?

Given a menu tree:

Items
--> Bat
--> Ball
------> Large Ball
------> Medium Ball
------> Small Ball
--> Bottle

If the user is on the Large Ball page, I'd like to be able to render all siblings. In my real case, they would be rendered as tabs. #84 may be the same thing, but it seems like that use case is actually rendering all the ancestors as well.

It seems I can do this:

{{ knp_menu_render(['AcmeDemoBundle:Builder:mainMenu', 'Items', 'Ball']) }}

But that would require that I know the name of the current menu item. If I can get that in twig somehow, I guess that would be fine and I could just fill it in there, but ideally there'd be some baked in functionality to support rendering sub-trees.

bkosborne avatar Jun 11 '14 19:06 bkosborne

Can you give an example where it can be useful ?

I'm trying to imagine "why".

Notice that there's an easy way to do but we were talking about deleting it because it's a problem in the code to implemente more cool things.

Nek- avatar Jun 12 '14 17:06 Nek-

Sure. I want to limit my main nav bar to just show two levels of depth in the menu, but the menu may be much deeper. When someone is on one of those pages, I want to introduce another navigation in a sidebar that contains the remaining pages.

Here's an example of a website that does it: http://www.bu.edu/cs/undergraduate/computer-science-major

The main menu on the top contains two levels of items. But if you click on Undergraduate for instance, there's a new nav on the right that contains remaining children.

bkosborne avatar Jun 12 '14 17:06 bkosborne

Has anyone figure out how to do this or is a custom template required? It appears I can limit the depth in using the depth option but I can not start at a specific depth. With knp_menu_get_current_item() I can start at the depth of the current page but I would like to always start the second depth of the root. Also what does matchingDepth actually do? I can't find any doc on it and it appears to do nothing at all.

sschueller avatar Dec 23 '16 10:12 sschueller

I also need this and it seems there is no really alternative to knpmenu on symfony. I also can't find out what matchingDepth does. Has anyone found a solution in this 5 years the bug is open now?

SevenbitsIt avatar Oct 08 '20 06:10 SevenbitsIt

I also can't find out what matchingDepth does.

See https://github.com/KnpLabs/KnpMenu/blob/master/doc/01-Basic-Menus.md#other-rendering-options

matchingDepth: The depth of the scan to determine whether an item is an ancestor of the current item

garak avatar Oct 08 '20 07:10 garak