Render multiple views


I am trying to render two views at once. Is this even possible or maybe there is an easier/better way of achieving this?

In this topic I read that someone suggested using the renderPartial() method.

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

	$this->renderPartial('view2', array('model' => $model));

However, if I use the following code in my controller action the first one gets rendered and the second one gets displayed beneath the other one without any styles / layout applied to. This is not really what I wanted ;)

Of course it is good that it is getting displayed beneath the first view but how do I get the layout / styles applied to the second view since it is rendered with renderPartial().

Maybe there is even a better way in achieving this despite rendering two views?

Thank you very much in advance :)

I suggest you to read my faq about the render life cycle…

After finish reading it you will understand what to do

Thank you for this link, dckurushin.

I just read the article and according to it’s last paragrap I should now be a [quote=‘’ timestamp=‘1317996592’]pro in layouts

Well, it definitely made some things more clear to me. However I still do not quiet understand how to render two views.

As of what I’ve read once the render() method is called there is now way to sneak in a second view. Only with the beforeRender and afterRender it can be modified but can I use those to insert a second view?

I still don’t think that my approach is the right way… Do I have to write the second view as a widget to implement it in the first one?

You can render view inside a view…

And also you can render a view, capture it, and pass it ass parameter for different view like it is done in the $this->render with the layout

Thanks, I simply put the following line in the view where I wanted the output to be. That’s exactly what I wanted!

$this->renderPartial('view2', array('model' => $model));

This sounds interesting! How would I do this?

As far as I understood the tutorial this is the line where the layout gets captured:


Would I use it for view2 like that?


And then pass $capturedView on to view1 within the render() method where I would output it with an echo?

To understand how, see the wiki article,

If you understand how your view is transfered to the layout, you will udnerstand how to capture a view

Best is looking at CController::render as in the wiki article, this is done there

layout is just simple view file

Great, thanks again!

Here is what I made it work :)

In the controller in the appropriate action I added the following:

$outputView2 = $this->renderPartial('view2', array('model' => $model), true);

$this->render('view1', array('model' => $model, 'outputView2' => $outputView2));

In the view file view1.php I simply echo’ed outputView2 where I wanted it to appear.

Thanks a lot for guiding me to these two scenarios, dckurushin!!

I decided to stick with the last one, described in this post, as it seems cleaner to me.

This is awesome!

jrn, you are asking the exact thing i want to have, and showing a great example. :)