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?
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
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
MyController.php
function actionIndex()
{
$model = new NewUserForm();
$users = User::model()->findAll(); // fetch from db for example
$this->render('index',array('users' => $users,'model' => $model));
}
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?