-
Notifications
You must be signed in to change notification settings - Fork 180
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
Easy Divider in KNP Menu #323
Comments
Dividers are child elements, because that semantically best matches how dividers are represented in the HTML by Bootstrap. Of course it would be possible to add dividers to items, but I don't really see the use. It would be a little bit less code, but logically it is nicer to have them as separate items. Look at this simplified example: $menu->addChild('item1');
$menu->addChild('item2');
$menu->addChild('divider', ['divider' => true]);
$menu->addChild('item3');
$menu->addChild('item3'); matches nicely the generated HTML <li>item 1</li>
<li>item 2</li>
<li class="divider"></li>
<li>item 3</li>
<li>item 4</li> |
It would be more "human" logic to put those in de code directly, but those dividers still need to be uniquely named. Sure, it is possible to auto-increment the divider-nr. but I think it's really easier just to code in a true or false on the append or prepend option. |
You can also create your own declare(strict_types = 1);
namespace AppBundle\Menu\Item;
use Knp\Menu\FactoryInterface;
use Knp\Menu\MenuItem;
final class DividerMenuItem extends MenuItem
{
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
parent::__construct('divider', $factory);
$this
->setExtra('divider', true)
->setAttribute('class', 'separator')
;
}
} Then, on your builder: $menu->addChild(new DividerMenuItem($this->factory)); And on my personal theme override: {% block spanElement %}
{% import 'knp_menu.html.twig' as knp_menu %}
{% if not item.extra('divider') %} {% Don't show menu content if it's a divider %}
<span>{{ knp_menu.attributes(item.labelAttributes) }}>
{% if item.extra('icon') is not empty %}
<i class="{{ item.extra('icon') }}"></i>
{% endif %}
{{ block('label') }}
</span>
{% endif %}
{% endblock %} Not directly linked to this bundle this might help. |
When using dividers you need to use unique divider names as child-elements.
Would it also be posible to set an attribute to add a divider to an existing normal child-element, like this:
Or
The text was updated successfully, but these errors were encountered: