4

I'm newbie to yii and I want to know:

How to apply individual classes(CSS) for zii.widgets.CMenu labels?

My code:

$this->widget('zii.widgets.CMenu',array(
                          'items'=>array(
                                 array('label'=>'Home', 
'url'=>array('/site/index')),<br>
                                 array('label'=>'About', 
'url'=>array('/site/page', 'view'=>'about')),<br>
                                 array('label'=>'Contact', 
'url'=>array('/site/contact')),<br>
                                 array('label'=>'Supplier', 
'url'=>array('/supplier/index')),<br>
                                 array('label'=>'Login', 
'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                                 array('label'=>'Logout 
('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), <br>
'visible'=>!Yii::app()->user->isGuest)
                         ),<br>
                 ));
lin
  • 17,956
  • 4
  • 59
  • 83
rdanusha
  • 913
  • 3
  • 15
  • 24
  • you will find your answer from here. http://www.yiiframework.com/wiki/525/customizing-the-cmenu-widget/ – Ranidu Jul 08 '14 at 06:25

3 Answers3

2

For li use itemOptions and for a href use linkOptions

For a href

array('label'=>'Link Lable',
      'url'=>array('controller/action'),
      'linkOptions'=>array("id"=>"link-id","class"=>"your-class1 your-class2 your-class3"), 
      'visible'=>!Yii::app()->user->isGuest)

For li

'itemOptions' => array('class' => 'your-class1 your-class2'),
Arslan Butt
  • 775
  • 1
  • 8
  • 20
2

You can supply itemOptions according to the official documentation that can be found here.

itemOptions, according to the documentation, are "Additional HTML attributes to be rendered for the container tag of the menu item."

This means they can be used to add HTML attributes to your items, as well as li elements like so:

$this->widget('zii.widgets.CMenu', array
(
    'items' => array
    (
        array
        (
            'itemOptions' => array('class' => 'class names here'),
            'label' => 'Home', 
            'url' => array('/site/index')),
        ),
    ),
));

Alternatively you can use linkOptions to add classes to the a href elements themselfs like so (note the change from itemOptions to linkOptions)

$this->widget('zii.widgets.CMenu', array
(
    'items' => array
    (
        array
        (
            'linkOptions' => array('class' => 'class names here'),
            'label' => 'Home', 
            'url' => array('/site/index')),
        ),
    ),
));
Dennis Jamin
  • 398
  • 1
  • 10
1
array(
    'label'=>'Home',
    'url'=>array('/site/index'),
    'itemOptions'=>'my_class'
)

itemOptions used to customize your li attribute.

ineersa
  • 3,445
  • 30
  • 40