Understanding render() and renderPartial()


I am currently trying to understand the render() and renderPartial() functions.

I have a layout file, lets say with a header, a footer, a content (as $content) and three flexible content boxes for e.g. recent News or this kind ($box1, $box2, $box3). These boxes can be filled with whatever.

Now we have Views, Widgets and Portlets. And here are my questions:

As far as I understood will the view be taken automatically from the controller’s method name (e.g. Edit), be rendered by calling render() and be put into $content. This is what happens automatically, right? I don’t have to specify where the view should be put into.

But what about the boxes? Do I have to run renderPartial(), receive the output into a variable as string and then hand over to the render() method as parameters?

Is my understanding correct so far?

Thanks for help!

render(‘myview’) will render the file ‘myview’, complete with the $layout that’s specified in your controller.

renderPartial(‘myview’) will render ONLY the contents for ‘myview’.

So, in a view ‘home’, you can have several renderPartial(‘box-left’) etc.

You can also supply parameters, and flags to return content instead of outputting. The last parameter for both methods is used to indicate if you want to process any client script.

Check out the blog demo that ships with Yii for additional samples :)

Thanks for your reply.

So what you are saying is that I have to run renderPartial() inside the view file for each box I want to render and render() inside the controller?

What I understand is that renderPartial() will not put the defined layout around. So if I run renderPartial(‘Edit’) the ‘Edit’ view will be shown without a layout or just rendered and “prepared” for output in a layout?

For me it’s really difficult to understand as I have a very different thinking seems.

Yeah, that’s pretty much correct. renderPartial(‘edit_form’) would just render the actual edit form.

For example


function actionIndex()


    $model = new NewUserForm();

    $users = User::model()->findAll(); // fetch from db for example

    $this->render('index',array('users' => $users,'model' => $model));


index.php view

<h2>user overview</h2>

<?= $this->renderPartial('userList',array('users' => $users));?>

<h2>new user form</h2>

<?= $this->renderPartial('newUserForm',array('model => $model));?>

renderPartial is also nice when you want to load HTML fragments with ajax, since you only get the HTML for the view you want.

Ok thanks. Now it’s more clear. So if I want to replace a box which contains either a login form or the name of the person logged in, where would you put this?

You would define the use of a portlet inside the layout, right?

You can use ajax to replace it for example.

<div id="somediv"></div>

<?= CHtml::ajaxSubmitButton('label',




		'data'=>array('somevalye' =>123),

		'replace' => '#somediv',                                   




Just have SiteController->actionForm use renderPartial:)

The blog demo explains some of these concepts in detail :)

Yes, I will read it for sure asap. With your explanation its more easy for me now I guess.