Bernard Lechler
Bernard Lechler

Reputation: 295

Yii - YiiStrap - TbNavbar - set active menu item

I'm using Yiistrap (http://www.getyiistrap.com/) on my site. I am using the TbNavbar widget in my theme. However, I can't seem to figure out how the various menu items are supposed to become active based on the page you are on. Is it supposed to happen automatically based on the URL that you are at, or do I have to write a function that looks at the current URL and explicitly set the given menu item to active?

$arNavbar = array(
  'brandLabel' => false,
  'collapse' => true,
  'display' => null, // default is static to top
  'items' => array(
    array(
        'class' => 'bootstrap.widgets.TbNav',
        'items' => array(
            array('label' => 'Home', 'url' => '/', 'active' => true),
            array('label' => 'Docs', 'url' => '/docs'),
            array('label' => 'Stuff', 'url' => '/stuff'),
            array('label' => 'Things', 'url' => '/things'),
            array('label' => 'About', 'url' => '/about'),
        ),
    ),
  ),
);

Home is always active

....
        'items' => array(
            array('label' => 'Home', 'url' => '/'),
            array('label' => 'Docs', 'url' => '/docs'),
            array('label' => 'Stuff', 'url' => '/stuff'),
            array('label' => 'Things', 'url' => '/things'),
            array('label' => 'About', 'url' => '/about'),
        ),
....

Nothing is ever active

Upvotes: 0

Views: 1049

Answers (1)

akos
akos

Reputation: 2644

Add URL-s as arrays. In this case, the first array element refers to the controller route, and the rest key-value pairs refer to the additional GET parameters for the URL.

For example:
array('index','param1'=>2) is a route to the 'index' action of the current controller. array('site/index','param1'=>2) is a route to the 'index' action of the 'site' controller.

So in your case use this:

'items' => array(
        array('label' => 'Home', 'url' => array('controller/home')),
        array('label' => 'Docs', 'url' => array('controller/docs', 'document_id'=>4)),
        ...
    ),

This way the menu items will be active, if you are at the specified controller/action.
Alternatively you can tell the 'active' parameter to be 'true' at specified controllers or actions this way:

'items' => array(
        // This will be active only on the 'site/home' action - this is the default if you give the URL in an array format
        array(
            'label' => 'Home',
            'url' => array('site/home'),
            'active' => (Yii::app()->controller->id == 'site' && Yii::app()->controller->action->id == 'home') ? true : false,
        ),

        // this will be active at every action of the 'docs' controller
        array(
            'label' => 'Docs',
            'url' => array('docs/index'),
            'active' => (Yii::app()->controller->id == 'docs') ? true : false,
        ),

        // this will be active at the 'view' action of every controller
        array(
            'label' => 'Stuff',
            'url' => array('stuff/index'),
            'active' => (Yii::app()->controller->action->id == 'view') ? true : false,
        ),
        ...
    ),

Happy coding!

Upvotes: 2

Related Questions