KnP Menu Bundle questions

1,341 views
Skip to first unread message

Neil Ferreira

unread,
Jun 14, 2012, 7:32:05 AM6/14/12
to symfony-...@googlegroups.com
I've got two queries about the KnP Menu Bundle, I know this may not be the correct place to ask, but if anyone does know anything please feel free to assist.

1. Since the homepage node is /cms/menu/main, how can I make it so it is displayed in the navigation? When I render the nav it only renders all the children of the 'main' node (main node creation: https://github.com/symfony-cmf/cmf-sandbox/blob/master/src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadMenuData.php#L43)

Ultimately the menu bundle should have a 'include root' option, but the documentation doesn't seem to mention it.


2. I've created a main navigation for my website. My content is outlined as such:
Homepage (/menu/main)
-- Contact us (/menu/main/contact)
  -- Map (/menu/main/contact/map)
-- About us (/menu/main/about)
  -- Staff list (/menu/main/about/staff)

I am using the following code:
{{ knp_menu_render('main', {'depth': 1, 'currentAsLink': true}) }}

This will display one level of nodes underneath 'main', for example: Contact - About us

What I need to do on the inner pages, such as on About us is render all the sub-pages of that section alone.   If you visit the "About us" page, all you should see is "Staff list",  so it would be some derivative of the 'depth' field, where we're setting the base node.


Thankyou. 

Uwe Jäger

unread,
Jun 14, 2012, 7:53:59 AM6/14/12
to symfony-...@googlegroups.com
Hi,

from my understanding of the menu bundle, main is the menu, and it's
children are the items displayed in the menu. Maybe this becomes
clearer when you consider the scenario where you have more than one
menu, eg in the site's footer. So you should actually change your
structure to have an menu item called "home-item" as sibling of
contact and about.

Your request would result in one item "main" being display with
children contact and about.

Not sure how to do the other stuff but I think it's possible. Just
look at the bundles code and docs.

Cheers
Uwe

2012/6/14 Neil Ferreira <ro...@neilf.net>:

Petesiss

unread,
Jun 14, 2012, 9:41:44 AM6/14/12
to symfony-...@googlegroups.com
Re the second point. If you just want to show a menu of children for your current page (e.g. show staff-list when on the about us page), then you do as you pasted in irc - just get the menu item for current page and use that as the menu root when you render. You can still define things like depth etc and it should only show the children of that node down to the depth you specify.

Pete

Neil Ferreira

unread,
Jun 14, 2012, 10:46:28 AM6/14/12
to symfony-...@googlegroups.com
Hey,

I'm trying this right now:
{% set currentItem = knp_menu_get('main').currentItem %}
{{ knp_menu_render(currentItem) }}

This works fine to show the children of that page, but now I have this problem:

IA:
Homepage (/menu/main) 
-- Contact us (/menu/main/contact) 
  -- Map (/menu/main/contact/map) 
-- About us (/menu/main/about) 
  -- Staff list (/menu/main/about/staff)  

If I go to the 'About us' page, Staff list shows up, which is fine,  when I go to the 'Staff list page' nothing shows up, and there is no way to go back to the node above it.

What I would need to do is something like:
{% set currentItem = knp_menu_get('main').currentItem %}
{% set breadcrumbs = currentItem.breadcrumbsArray %}
{{ knp_menu_render(breadcrumbs[1]) }}

Unfortunately I can't access [1] from that array in twig, unless I'm doing something wrong.  If I did that then I would always have subnavigation start right below the homepage.

Cheers

Neil Ferreira

unread,
Jun 14, 2012, 10:48:01 AM6/14/12
to symfony-...@googlegroups.com
damnit, Ignore what I said about using the breadcrumbs array, it returns a label => uri pair.  Which won't help me.  I might need to do a PR to get this to work I think :|

Petesiss

unread,
Jun 14, 2012, 11:00:49 AM6/14/12
to symfony-...@googlegroups.com
I have had that problem for ages where I cant render a menu that is ALWAYS a level 2 menu, even if I dive deeper in to the tree.

To get round it I wanted to add a getAncestor($level) method to the menu item so that you could always retrieve your parent at a specified level. So you can do something like:


      {% set current = knp_menu_get('main').getCurrentItem() %}

      {% set root = current.ancestor(level) %}

      {{ knp_menu_render(root.setChildrenAttribute('class', class) , {'depth': depth, 'currentClass' : 'active', 'ancestorClass' : 'active'}) }}


It looked like it might be quite slow though on really deep sites so didnt get merged. Im not convinced though as it doesn't do anything different to getLevel which is in there. 

We've just been using it in our own branch with it in since then... hoping for a better solution to emerge at some point in the future.

As an alternative to getAncestor, you could do similar logic in twig using the getLevel method available, so you can iterate up the parents till you get the level you are looking for and render that item.

If anyone else reads this and knows the perfect solution, please share.

See https://github.com/KnpLabs/KnpMenu/issues/26 for the previous discussion.

Pete


On Thursday, 14 June 2012 15:46:28 UTC+1, Neil Ferreira wrote:
Hey,

I'm trying this right now:
{% set currentItem = knp_menu_get('main').currentItem %}
{{ knp_menu_render(currentItem) }}

This works fine to show the children of that page, but now I have this problem:

IA:
Homepage (/menu/main) 
-- Contact us (/menu/main/contact) 
  -- Map (/menu/main/contact/map) 
-- About us (/menu/main/about) 
  -- Staff list (/menu/main/about/staff)  

If I go to the 'About us' page, Staff list shows up, which is fine,  when I go to the 'Staff list page' nothing shows up, and there is no way to go back to the node above it.

What I would need to do is something like:
{% set currentItem = knp_menu_get('main').currentItem %}
{% set breadcrumbs = currentItem.breadcrumbsArray %}
{{ knp_menu_render(breadcrumbs[1]) }}

Unfortunately I can't access [1] from that array in twig, unless I'm doing something wrong.  If I did that then I would always have subnavigation start right below the homepage.

Cheers


On Thu, Jun 14, 2012 at 9:41 PM,
Re the second point. If you just want to show a menu of children for your current page (e.g. show staff-list when on the about us page), then you do as you pasted in irc - just get the menu item for current page and use that as the menu root when you render. You can still define things like depth etc and it should only show the children of that node down to the depth you specify.

Pete

On Thursday, 14 June 2012 12:53:59 UTC+1, Uwe Jäger wrote:
Hi,

from my understanding of the menu bundle, main is the menu, and it's
children are the items displayed in the menu. Maybe this becomes
clearer when you consider the scenario where you have more than one
menu, eg in the site's footer. So you should actually change your
structure to have an menu item called "home-item" as sibling of
contact and about.

Your request would result in one item "main" being display with
children contact and about.

Not sure how to do the other stuff but I think it's possible. Just
look at the bundles code and docs.

Cheers
Uwe

2012/6/14 Neil Ferreira 
On Thu, Jun 14, 2012 at 9:41 PM, Petesiss wrote:
Re the second point. If you just want to show a menu of children for your current page (e.g. show staff-list when on the about us page), then you do as you pasted in irc - just get the menu item for current page and use that as the menu root when you render. You can still define things like depth etc and it should only show the children of that node down to the depth you specify.

Pete
On Thursday, 14 June 2012 12:53:59 UTC+1, Uwe Jäger wrote:
Hi,

from my understanding of the menu bundle, main is the menu, and it's
children are the items displayed in the menu. Maybe this becomes
clearer when you consider the scenario where you have more than one
menu, eg in the site's footer. So you should actually change your
structure to have an menu item called "home-item" as sibling of
contact and about.

Your request would result in one item "main" being display with
children contact and about.

Not sure how to do the other stuff but I think it's possible. Just
look at the bundles code and docs.

Cheers
Uwe

2012/6/14 Neil Ferreira  
Reply all
Reply to author
Forward
0 new messages