-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rootAttributes rendering option #166
Comments
👍 |
👍 I've searched up and down for a way to add a class to the root element from knp_menu_render, but come up with nothing. Is this truly not possible currently? It seems like this should be a standard feature of the library. If this capability does not exist, I would definitely encourage the addition of a I'd be happy to do the work to accomplish this, if necessary. |
It seems the main issues are here:
This means that even if you define a Builder as such: $menu = $factory->createItem('root', $options);
$menu->setAttribute('class', 'root-class');
$menu->setAttribute('id', 'root_id');
$menu->setChildrenAttribute('class', 'child-class'); ... the
I assume this was done to enable root-level setting of child attributes, and it can be overcome fairly easily with a custom renderer or by extending the base twig template. If this is not the reason why, I am definitely curious to hear it. |
This is because |
@stof: Ah excellent! Thank you for the clarification. Is this mentioned somewhere in documentation that I missed? |
Thank you, sorry for commenting on such an old issue. |
wether problem was solved? because I have same use case as @dirkluijk mentioned. And from the code I see that is impossible to pass attributes from twig. |
@aur1mas
Note that the second way does not work when registering menu themselves as services (which has several drawbacks, including this one). You need to use either the convention-based provider ( |
👍 Need an option to override the root class from renderer function |
I gave 2 solutions just above, which don't require new features (and don't require adding new responsibilities in the renderer) |
I myself tried to do this just now. KnpMenu/src/Knp/Menu/Resources/views/knp_menu.html.twig Lines 17 to 29 in e11c6cd
I have 2 questions:
IMO documenting the |
@martixy the answer to this question is given in a previous comment in this discussion: #166 (comment) |
If menu as a service and need to be used with two different ways, by exemple one render with class on ul and do nothing on the other render. do menu.setChildrenAttribute() will work on the service and no on only the display. Actually the only way is to make a template for each render with special need... :( |
Hi,
I want to set the root attributes of a menu from inside a Twig template. I am working on a project where different themes need to have different CSS classes for the menu, and it seems quite ugly to override the
MenuBuilder
just to change thechildAttributes
of the root item.My suggestion is to add a
rootAttributes
option to the default renderers.Example usage (Twig renderer):
If you agree, I could submit a PR with all necessary changes to the default renderers, docs and twig template.
The text was updated successfully, but these errors were encountered: