Layout with dynamic block

The use of the params property to allow communication between view and layout, is advisable for simple cases, but there are some more complex cases where we must share the block of HTML.

For example, think about the advertising box in layout (usually left or right column of the template), that could change according to the view that is being displayed.

In this case, we need to pass the entire block of HTML code from view to layout.

For this purpose, this framework provides Block statements, where we can define entire blocks of data to send from view to layout.

Using Blocks means to define the Block statement in view and display it in another view, usually layout.

We define the Block statement in view as follows:

<?php $this->beginBlock('block1'); ...

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.