[resolved] How To use content inside CWidget

I've read this in the Guide:

<?php $this->beginWidget('path.to.WidgetClass'); ?>

...body content that may be captured by the widget...

<?php $this->endWidget(); ?>

How do i use this content?? Is there a way to use it?


You can call ob_start() in init() and then get the body content in run() by using ob_get_clean()

It doesn't work, the text has been writed twice.

example code:

class ClockRefresh extends CWidget {

    public $time = 1000;

    public $url = null;

    public $updateAtStart = false;

    public $containerCssClass = null;

    public function init(){



    public function run(){

        $id = $this->getId();

        echo "<div id="$id"";

        if ($this->containerCssClass){

            echo ' class="' . $this->containerCssClass . '"';


        echo '>';

        echo ob_get_clean();

        echo ('</div>');



And the extension call is:

<?php $this->beginWidget('application.extensions.timerext.ClockRefresh', array(



    'updateAtStart'=>true)); ?>


<?php $this->endWidget();?>

it shows: loading…


<div id="yw0"></div>

instead of:

<div id="yw0">loading...</div>

Your logic is not right here because you are also capturing the tags you render in run(). You should move those code before ob_start().

It Works!!!

Thanks Qiang!!!

Sorry to reopen this post but I have problems with my extensions, I cannot render two instances of an HTML layer from a Class in a view (http://www.yiiframework.com/extension/egmap/) explained a workaround, but why I cannot render two HTML layers even if I create two different objects from the same class?

Mind sharing your solution Sebas? Why did it work?


Forget my last post, I have already found a solution and also mdomba pointing me for some strange HTML behaviour with GMaps. Nothing to do with ob_get_contents

Thanks anyway