The quickest guide for porting Yii CWidgets to CPortlets

Submitted by boaz on Wednesday, May 9, 2012.

I've implemented several widgets thus far in a design-free project I'm working at the moment (graphical designer has not been contracted yet...). One day, I thought that even for this  a-la prototype site I could make the widgets a little more beautiful - just make the widgets "CPortlets".

So I quickened to action and CPortlet being a CWidget, I thought I would merely update the class definition (meaning the "extends..." part of it), set up a title, and viola! Everything is now a little more sexy and unified. 
Wrong. Something was really weird - I had the entire site rendered twice - once in the main window (viewport) and another one in the portlet. Don't ask... :) .

The problem was in the methods of the portlet class. Below is the "full" extremely quick guide summarizing those changes:

In short, to port your "boxes" from CWidgets to CPortlets, do:

  1. Update class definition: from extends CWidget to extends CPortlet. Well that's obvious... :)
  2. In the class itself, refactor (rename) run() to renderContent(). I have only tested this where the content of this method is a simple $this->render("view_filename") so YMMV.
  3. In the same class, method init(), add as its final statement: parent::init();

See! I told you it will be quick! :)

Leave a Comment

Fields with * are required.