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))




ob_start();

$foo=$this->render('view',null,true);

ob_end;




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){

$this->foo=$view;

return false;

}


public function actionStart(){

$this->redner('yourview');

}




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


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

(untested)

/Tommy

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




ob_start();

$this->render('view',null,true);

$contents = ob_get_contents();

ob_end_clean;



Set the third parameter of render or renderPartial to true.

Edit:

As already pointed out by diggy and tri. :)