Question

i am using partials to decorate Zend_Navigation to fetch desired output. everything is working good except i am having difficulty adding class="active" in <a href..> tag. here is my layout partial sidebar.phtml

<ul id="menu" class="nav">
<?php foreach($this->container as $page): ?>
    <li class="<?php echo $page->class; ?>"><a href="<?php echo $page->getHref(); ?>"><span><?php echo $page->label; ?></span></a></li>
<?php endforeach; ?>
</ul>

in the <a href="<?php echo $page->getHref(); ?>"> i want to add class="active" for the current page.

i tried some solutions which i found after searching. but nothing worked for me.

most of the solutions talk about doing it in controller for example

$page = $this->view->navigation()->findOneByLabel('Your Label');
if ( $page ) {
  $page->setActive();
}

i haven't tested this yet. as i am using multiple navigations in the layout from one single navigation.xml file. i was wondering if is there a way i could do it in partials itself instead of in controllers or other helpers?

thank you

Was it helpful?

Solution

It's easy. In the view partial you can check which page is active:

if ($page->isActive()) { ... }

From ZF documentation:

Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top