Trying to use CPortlet for submenu with dynamic content

I’ve seen a few posts questions over the last few years asking how to build a dynamic submenu.

I came across an article on your blog here on how to use CPortlets to build a user menu, which is great but it’s using static html.

So I tried to use this logic to build a widget that was dynamic, such that you can define the sub-menu items in the controller.

The problem being though, that the submenu wouldn’t be part of the content and would therefore need to be rendered by the layout.

I’ve tried creating a submenu Portlet widget and then using a callback function, to the controller to get the menu items.

When I trace through it looks like it is working but the menu is not being rendered.

I’m wondering whether the layout has already been rendered by the time the controller callback has been called.

Any ideas?

Here is the code:




class SubMenu extends CPortlet


    public $title;

    public $menu;


    public function init()



        $controllerId = Yii::app()->controller;



        if (method_exists($controllerId, 'getSubMenu')) {

            $menu= $controllerId->getSubMenu();





    protected function renderContent()


        if (isset ($menu)) {

            $this->render('subMenu', array('menu'=>$menu));







        foreach ($menu as $menuName=>$menuLink) {

            echo "<li>".CHtml::link($menuName,array($menuLink))."</li>";






public function getSubMenu() {



            if(UserModule::isAdmin()) {

                $mu=UserModule::t('Manage User');


                $subMenuItems[UserModule::t('List User')]='/user';




            $subMenuItems[UserModule::t('Change password')]='changepassword';




            return $subMenuItems;





   <div id="submenu">

        <?php $this->widget('SubMenu'); ?>


You should use $this->menu instead of $menu in the CPortlet code:

 if (method_exists($controllerId, 'getSubMenu')) {

            $this->menu= $controllerId->getSubMenu();



 protected function renderContent()


        if (isset ($this->menu)) { 

            $this->render('subMenu', array('menu'=>$menu));



Brilliant - thanks!

Also need to make the same change in the CPortlet renderContent method


protected function renderContent()


        if (isset ($this->menu)) {

            $this->render('subMenu', array('menu'=>$this->menu));



can someone please help me out…

m trying to create a menu like this…




table 2-items

values-Name,type,property ,cost

tem Ordering - Javascript, HTML5 & PHP

  1. Create list of shop names on a page by fetching values from db

  2. hovering over a shop will display address,contact and a link "items."

3.On clicking on items ,it should display th list of items in that shop…

any example of this or something like this…

even a drop down menu will work…

i m new to yii so dont know how about the interface…

its all confusing to me as for now…