Search code examples
phptwitter-bootstrapzend-frameworkomeka

Set CSS class on second level UL with Zend framework and bootstrap


The following code will set the class nav on the first level UL

$mainNav = public_nav_main();
$mainNav->setUlClass('nav')->setUlId('main-menu-left');

However im using bootstrap and so want the second level ul to have the class 'dropdown-menu'

I cant seem to find a reference to get this sorted.

Zend is being used as the base structure in the software im using, Omeka. Unfortunately Omeka doesnt have a way to do this natively so I am having to dive into the underlying Zend FW although I dont want to modify that too much as it might be changed.


Solution

  • This is admittedly an ugly hack, but you could do it by processing the output of public_nav_main() with a regular expression. So in the header.php file you would replace:

    echo public_nav_main();
    

    with

    echo preg_replace( "/(?<!\/)ul(?!.*?nav)/", 'ul class="dropdown-menu"', public_nav_main() );
    

    This will only work if you only have 2 levels in your menu, since the above regular expression will also add the class="dropdown-menu" to all ul elements below the top level ul.

    Benefits

    • Simple
    • Achieves what you want to do
    • Doesn't require writing helpers or modifying the underlying framework
    • Should not break when the underlying framework is updated unless the update renders something other than a string from public_nav_main()

    Downsides

    • Won't work if your menu has more than two levels
    • Will result in having two class attributes in your 2nd and lower level ul elements in the event that they already have a class attribute