Splitting the common view content into reusable views
Sometimes, views share the same common portion of content. In the examples made until now, we have seen that a common area for itemsList
and itemDetail
could be copyright data, which displays a disclaimer about copyright info.
In order to make this, we must put the common content in a separate view and call it using the renderPartial()
method of controller (http://www.yiiframework.com/doc-2.0/yii-base-controller.html#renderPartial%28%29-detail). It has the same types of parameters of the render()
method; the main difference between the render()
and renderPartial()
methods is that render()
writes a view content in layout and renderPartial()
writes only view contents to output.
Example – render partial in view
In this example, we create a common view for both itemsList
and itemDetail
about copyright data.
Create a view file named _copyright.php
in views/news
.
Note
Usually, in Yii2's app, a view name that starts with underscore stands for common...