December 07, 2014

December 07, 2014
In this article, we are going to discuss about the two controller methods in YII, render and renderPartial. Both the render and renderPartial methods are used to creating the controller's view content. Simply put this is the information the view will receive from the controller.

The difference between render and renderPartial is that the latter will not load the layout. This is useful in case you'd like to have a popup window or ajax functionality which shows short information.

Here is an example with a whole method:

public function actionView($slug) { 
    $model = $this->loadSlug($slug);
    $category = $this->loadCategory($model->category); 
    $this->render('view', array( 'model' => $model, 'category' => $category->category )); 

The first argument for render is view. This is the view file to which will be added .php. This should be the file protected/views/view_name/view.php.

The next interesting thing is the array which comes as second argument. This is how you set the properties or variables that will be sent to the view and will be available there. In our case we will have $model (array) and $category at our disposal in the view file.

Eventually you could create variables in the view and you don't have to necessarily pass them from the controller. However, this will not be in the spirit of MVC.