Using blocks
One of the Yii features you can use in your views is blocks. The basic idea is that you can record some output and then reuse it later in a view. A good example would be to define additional content regions for your layout and filling them elsewhere.
In the previous version, Yii 1.1, blocks were called clips.
Getting ready
Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
How to do it…
For our example, we need to define two regions in our layout—
beforeContent
andfooter
.Open
@app/views/layouts/main.php
and insert the following code line just before the content output:<?php if(!empty($this->blocks['beforeContent'])) echo $this->blocks['beforeContent']; ?>
Then, replace the footer code with the following code:
<footer class="footer"> <div class="container"> <?php if (!empty($this->blocks['footer'])): echo $this->blocks...