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'); ?> ...content of block1... $this->endBlock(); ?>
Here, beginBlock
and endBlock
define the beginning and the end of the block1
named statement. This content is saved into the blocks
property of the view component with the block1
attribute.
We can access this block through $view>blocks[$blockID]
in every view, including layout.
To render a block in layout view, if available, use the following code:
<?php if(isset($this->blocks['block1']) { ?> <?php echo $this->blocks['block1'] ?> <?php } else { ?> … default content if missing block1 attribute <?php } ?>
Obviously, we can define all the blocks that we want.
In this example, we will see how to display, when available, a box with advertising info that displays data sent from view.
The first thing to do is to add a block in layout displaying data.
Enter in views/layouts/main.php
and change div
with container class as follows:
<div class="container"> <?= Breadcrumbs::widget([ 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], ]) ?> <div class="well"> This is content for blockADV from view <br /> <?php if(isset($this->blocks['blockADV'])) { ?> <?php echo $this->blocks['blockADV']; ?> <?php } else { ?> <i>No content available</i> <?php } ?> </div> <?= $content ?> </div>
We have added a div with the well
class to display the content of blockADV
, if available. If blockADV
is available in $this->blocks
, it will display its content; otherwise, it will display no content available
, as a courtesy message.
Now, we will create a new action in NewsController
, called advTest
, and then will create a brand new view.
Let's start off by creating a file in views/news/advTest.php
with the following content:
<span> This is a test where we display an adv box in layout view </span> <?php $this->beginBlock('blockADV'); ?> <b>Buy this fantastic book!</b> <?php $this->endBlock(); ?>
We can insert any content in a block; in this case, we have put in text.
Then, open NewsController
and add a new action advTest
:
public function actionAdvTest() { return $this->render('advTest'); }
Now, point the browser to http://hostname/basic/web/index.php?r=news/adv-test
and we will see the following screenshot:
All other pages will only show no content available
in the screenshot.
13.58.112.1