put the content of a view at a variable

Is there a way to pass the content of a view in a variable with out rendering?

It may be useful with ajax request.

Mmmm If you do not render a view, you have no content of a view. Or not?

I want this $a=$this->renderpartial(view);

I try is but it put the content of the view at the IE.

But I need to pass the content as json content,so it dies not work…may as json_encode(‘var’=>$this->renderpartial(view))




now variable $foo containts result of view template.

Or even better solution is to use BeforeRender function.

class FooController {

public $var;

public function beforeRender($view){


return false;


public function actionStart(){



For details look at code of CController.php, starting from line #775

json_encode('var'=>$this->renderpartial('view', array(...), true)) 



Check the documentation render and renderPartial CAN return the result instead of rendering it to the browser - http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail

Another way to do this is



$contents = ob_get_contents();


Set the third parameter of render or renderPartial to true.


As already pointed out by diggy and tri. :)