renderPartial in CGridView cell

I want to render a php view file in the cell of a CGridView.

Therefore I wrote this code:


$this->widget('zii.widgets.grid.CGridView', array(

  'dataProvider'=>$dataProvider,

  'columns'=>array(

      array(

          'value'=>array($this, 'renderPartial("_lineProblems")')

      ))));

But I get this error:

BookController and its behaviors do not have a method or closure named “renderPartial(’_lineProblems’ )”.

The stack trace shows this as the problem:


call_user_func_array(array(BookController, "renderPartial('_lineProblems' )"), array("data" => line, "row" => 0, 0 => CDataColumn))

I don’t understand the error. $this is an instance of BookController and BookController is a child of CController. CController has the methode renderPartial, so $this also has this method. How do I fix this?

I’m even more surprised that you could declare an array as the ‘value’ property… Doc says ‘string’

http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

Normally, $this used in the context of a component refers to that component. You may want to read this: http://stackoverflow.com/questions/11250342/basic-yii-concept-where-is-this-defined

Passing an array is a feature:

http://www.yiiframework.com/doc/api/1.1/CComponent#evaluateExpression-detail

And $this refers to the controller, because $this is called in the view, not in the component.

Good to know, thanks!

For the $this part, I guess I meant when someone would want to use it in a regular string value: ‘value’ => ‘$this->…’ then $this wouldn’t be the controller, but CDataColumn in this case.

Anyway, it’s not what you need, but I’ve learnt something new this morning, thanks.

I tried on some existing code, and doing this works:


     'value' => function(){

                    $this->renderPartial('_lineProblems');

                },

But layout disappears even with just render

The code you suggested doesn’t work here. I get this error:

Fatal error: Using $this when not in object context in view.php on line 112

Weird, it worked for me. Which PHP and Yii versions do you have?

This works for me:




$controller = $this;

...


    'value'=>function($data, $row) use ($controller){

                return $controller->renderPartial('_lineProblems', NULL, true);

          }




Thanks for your help.

I use Yii 1.1.9 with PHP 5.3.6.

Here 5.3.5 with 1.1.10 – maybe there’s a difference, too lazy to check :)

And your layout is ok with your syntax?

Yes, it works fine.