Still Questioning
Still Questioning

Reputation: 690

How do I apply class="current" to an <a href=...> tag when using Zend_Navigation?


As you can read from the title, I'm trying to add class="current" part to the HTML anchor tag that my zend_navigation renders.

Here's what i have:

Bootstrap.php

protected function _initNavigation(){
         $this->bootstrap('layout');
         $layout = $this->getResource('layout');
         $view = $layout->getView();
         $config = new Zend_Config_Xml(APPLICATION_PATH .'/configs/navigation.xml','nav');
         $navigation = new Zend_Navigation($config);
         $navigation->current()
         $view->navigation($navigation);
     } 

And then in the View script:

<div class="NavMenu">
  <?= $this->navigation()->menu(); ?>
</div>

I’m pretty sure there’s some standard and proper way of doing it, but after about couple hours of search, I’m unable to find my answer. Many thanks to you for your kind help.

Upvotes: 1

Views: 1672

Answers (2)

mapsi
mapsi

Reputation: 644

Stick this in your controller...

public function init()
{
            $uri = $this->_request->getPathInfo();          
            $activeNav = $this->view->navigation()->findByUri($uri);
            $activeNav->active = true;
            $activeNav->setClass("active");
}

For more information read the comments of the setClass method in Zend_Navigation_Page

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Navigation/Page.php

Upvotes: 5

Phil
Phil

Reputation: 164892

You could create your own menu view partial or if you're only interested in targeting links under the active menu item, try

li.active > a

in your CSS.

Upvotes: 0

Related Questions