How to set active class for current page

Wish you very happy new year to all

i have used the following code for menu




<ul class="main_menu">

      <li><?php echo CHtml::link('<span>Home</span>',array('index')); ?></li>

      <li><?php echo CHtml::link('<span>About us</span>',array('page&view=about')); ?></li>

      <li><?php echo CHtml::link('<span>Contact</span>',array('contact')); ?></li>

</ul>



im getting HTML out put as




<ul class="main_menu">

     <li><a href="/my/index.php?r=site/index"><span>Home</span></a></li>

     <li><a href="/my/index.php?r=site/page&amp;view=about"><span>About us</span></a></li>

     <li><a href="/my/index.php?r=site/contact"><span>Contact</span></a></li>

</ul>



I need to set class=active for home page if im in home page (like this for all pages) out put should be like this… help me




<ul class="main_menu">

     <li><a class="active" href="/my/index.php?r=site/index"><span>Home</span></a></li>

     <li><a href="/my/index.php?r=site/page&amp;view=about"><span>About us</span></a></li>

     <li><a href="/my/index.php?r=site/contact"><span>Contact</span></a></li>

</ul>



Thanks in advance

Why not use CMenu then?

Im using some special designs in menu

You can extend CMenu and write an own renderMenuRecursive() method. If you just need to know how CMenu highlights the current item, take a look at the isItemActive() method. If you’ll be satisfied with a very basic implementation, you can do the following:




<li><?php echo CHtml::link('<span>Home</span>', array('index'), ($this->id=='site' && $this->action->id=='index') ? array('class'=>'active') : array()); ?></li>



"$this" inside a view refers to the current controller.

thank you it’s work fine for index, but in the about us page its different how we could do that

Home =>site/index

about us=>site/page&view=about

you have given a code to find out id and action->id still i need to find out view too .$this->action->id only returns page. Help me

You will need to add a condition:




($this->id=='site' && $this->action->id=='page' && $_GET['view'] == 'about')



IMHO all these checks don’t look good, but if you like it… ;)

Thank you andy…its not good im accepted your point … could you give me any url which have complete code of cmenu extending …Wish you very happy new year

What exactly doesn’t satisfy you in the CMenu class? As I said before, take a look at the CMenu implementation (framework/zii/widgets/CMenu.php). In renderMenu() and renderMenuRecursive() all tags are generated. So you can create your own class Menu:




Yii::import('zii.widgets.CMenu', true);


class Menu extends CMenu

{

    protected function renderMenu($items)

    {

        // your code there.

    }

}



And I wish you good luck in studying Yii this year ;)