Using multiple layouts

During the building of a website or a web application, usually it could be required to render different views with different layouts. Think about, for example, the lists and details of news made in this chapter.

The layout is managed by the $layout property of Controller; main is the default value for this property.

Just set this property to change the layout file where to render the content of the view.

There are some important rules to write the value of the $layout property:

  • A path alias (for example, @app/views/layouts/main).
  • An absolute path (for example, /main) is where the layout value starts with a slash. The actual layout file will be looked for under the application layout path, which defaults to @app/views/layouts ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.