Search code examples
phpcsssymfonyknpmenubundle

Symfony 2 KNP Menu: add CSS class to link


I'm using the KnpMenuBundle for Symfony2 and I couldn't find a way to add a css class to the from the Menu generated links.

What I tried to set the class to the child attribute, but that will not be given to the link but to possible children menus (submenus).

    $menu->addChild('agb', array('uri' => '#'))
        ->setAttribute('divider_append', true)
        ->setChildenAttribute('class', 'childClass');

This will result into the following HTML

<li>
    <a href="#"> agb </a>
    <ul class="childClass">
        ....
    </ul>
</li>

But I need it like this:

<li>
    <a href="#" class="childClass"> agb </a>
    <ul>
        ....
    </ul>
</li>

How can I do this?


Solution

  • $menu->addChild('agb', array('uri' => '#'))
        ->setAttribute('divider_append', true)
        ->setLinkAttribute('class', 'childClass');
    

    easy as that :)