Problem Creating Cwidget

I am having a problem trying to create a simple widget. Debugging it, I found that it crashes when requiring the file in YiiBase.

Strangely enough, if I convert the CWidget to a CPortlet it works fine. This would seem to indicate that probably not a permissions or file path issue.

Here is the code that creates the widget in my view:

<?php $this->widget('TaskBar', array('action'=>$action)); ?>

Here is the CWidget code:

class TaskBar extends CWidget

{

public $action;





public function init()


{


}





protected function run()


{


    $this->render('_taskBar', array('action'=>$this->action));


}

}

Here is the code that works just fine:

Yii::import(‘zii.widgets.CPortlet’);

class TaskBar extends CPortlet

{

public $action;





public function init()


{


    parent::init();


}





protected function renderContent()


{


    $this->render('_taskBar', array('action'=>$this->action));


}

}

What am I doing wrong?

[color="#006400"]/* moved from "Bug Discussions" to "General Discussion for Yii 1.1.x" */[/color]

You have to call parent::init() :)

http://www.yiiframework.com/doc/guide/1.1/en/extension.create#widget

Thanks for the reply. I added the call the to parent::init() and unfortunately it had no effect. It still crashes while the widget is being created.

public function init()


{


    parent::init();


}
  • Scott

I’m sorry. I was wrong.

In fact we don’t need to call parent::init() … CWidget::init() does nothing.

http://www.yiiframework.com/doc/api/1.1/CWidget#init-detail

(Click on the "show" link to see the source.)

On the other hand, CPortlet is starting output buffering in init().

http://www.yiiframework.com/doc/api/1.1/CPortlet#init-detail

And it flushes the buffer in run().

http://www.yiiframework.com/doc/api/1.1/CPortlet#run-detail

So, how do you call your widget in your view?

If you are expecting your widget to function like this:




$this->beginWidget();

... contents ...

$this->endWidget();



Then you have to do the same thing as CPortlet does.