render() and renderPartial()

There’s a thread from way back in 2011 called ‘understanding render() and renderPartial()’. This new thread is pretty much a grandchild of that original post (which was probably talking about Yii1, but still leaves some questions for Yii2).

Here’s what I don’t get:

On various tutorials and in this forum, there seems to be agreement that:

‘render()’ - loads (displays) a file while wrapping it around a template.


'renderPartial() - does the same thing but DOES NOT wrap the view file around the template.

Can we all nod and say, "okay"? Good!

Well, if that’s the case, how come the auto generated CRUD code from Gii calls the form with…

<?= $this->render('_form', [

    'model' => $model,

]) ?>


This seems to be in contradiction to what the various tutorials out there are saying.

Now, if the answer is, “Oh, but it’s cool - we don’t need to worry about templates if we’re calling from a view”, then why are we even wasting our time with renderPartial() in the first place?

I’ll refrain from complicating matters further by discussing $this->context->renderPartial()

Can anyone point me in the right direction with this? I’m totally confused right now.

There is a difference between these methods when called in a controller as opposed to called from a view. The controller methods act just like what you described above. "render" includes the layout, "renderPartial" does not.

Now when you are in the context of a view, "render" will NOT wrap the output in a layout. Meaning it acts like the "renderPartial" in a controller.

Okay, thanks. That’s cool.

It seems like $this->context->renderPartial() (being called from a view) is pointless.

This is a surprise since it was the recommended method in a Yii2 book that I got. Anyway, no matter. Thanks again!

Did the book give a reason for its recommendation?