官术网_书友最值得收藏!

  • Yii2 By Example
  • Fabrizio Caldarelli
  • 456字
  • 2021-07-16 19:38:56

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'); ?>
...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.

Example – add a dynamic box to display advertising info

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.

Note

The position where block is defined in view is not important.

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:

Example – add a dynamic box to display advertising info

All other pages will only show no content available in the screenshot.

主站蜘蛛池模板: 观塘区| 涟源市| 苍溪县| 广宁县| 桂林市| 贺州市| 临高县| 合江县| 西华县| 清丰县| 连江县| 涿州市| 汾阳市| 沁水县| 富川| 磐石市| 晋州市| 沾益县| 广德县| 辛集市| 珠海市| 惠安县| 朝阳市| 南川市| 阿拉善左旗| 北京市| 西峡县| 德保县| 牙克石市| 香格里拉县| 崇礼县| 铜陵市| 淮滨县| 金川县| 永城市| 湘乡市| 察哈| 恩施市| 印江| 闸北区| 高唐县|