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

Sharing data between views and layout

Yii2 provides a standard solution to share data between views and layout, through the params property of the View component that you can use to share data among views.

Note

This is a standard solution since the params property exists in all views and it is attached to the View component.

This property, params, is an array that we can use without any restriction.

Imagine that we want to fill the breadcrumb element in the layout to track the path of navigation.

Open the main layout at views/layouts/main.php; you should find the default implementation of breadcrumb just before declaring the footer:

        <div class="container">
            <?= Breadcrumbs::widget([
                'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
            ]) ?>
         </div>

We need to fill the breadcrumbs property of params in view to display from any view to the layout custom path. For example, we want to display breadcrumbs in the SiteController index.

Go to views/site/index.php and add the following code at the top of the file:

$this->params['breadcrumbs'][] = 'My website';

Note

Since we are in view file, $this refers to View component.

Go to http://hostname/basic/web/index.php?r=site/index to see the breadcrumb bar appearing at the top of the page:

Sharing data between views and layout

Example – change the layout background based on a URL parameter

Another example of communication between view and layout is, for instance, to change the layout background color based on a URL parameter.

We need to change the background of route site/index passing the bckg parameter in URL.

Therefore, we must open views/site/index.php and put this code at the top:

<?php
$backgroundColor = isset($_REQUEST['bckg'])?$_REQUEST['bckg']:'#FFFFFF';
$this->params['background_color'] = $backgroundColor;

This code will set $backgroundColor to #FFFFFF (white color), if it is not passed to the bckg parameter, otherwise it will be passed a value.

Then, set the params attribute of View component in order to write its content in layout.

Open views/layout/main.php, and, in the body tag, apply the style based on params['background_color'] passed from view.

Then, let's change the layout of the body tag with the following:

<?php
$backgroundColor = isset($this->params['background_color'])?$this->params['background_color']:'#FFFFFF'; ?>
<body style="background-color:<?php echo $backgroundColor ?>">

Finally, go to http://hostname/basic/web/index.php?r=site/index&bckg=yellow to have a yellow background or to http://hostname/basic/web/index.php?r=site/index&bckg=#FF0000 to have a red one.

Note

In this example, we are setting the background property of params only in views/site/index.php. Other views do not set this property, so if we have not checked whether background_color property exists in the layout file, we will receive an error of missing the attribute from the framework, which means:

$backgroundColor = isset($this->params['background_color'])?$this->params['background_color']:'#FFFFFF';
主站蜘蛛池模板: 嵊州市| 元朗区| 通许县| 铜陵市| 台中县| 海门市| 景谷| 米泉市| 中宁县| 抚顺市| 霍邱县| 兴宁市| 昌乐县| 中西区| 乌苏市| 镇远县| 抚宁县| 台东市| 马关县| 天全县| 博罗县| 盐城市| 肃宁县| 长宁县| 仁寿县| 新安县| 陇西县| 仁化县| 西青区| 论坛| 泰和县| 垦利县| 米泉市| 乌拉特前旗| 洪泽县| 烟台市| 长沙县| 兴安盟| 新疆| 肇庆市| 三亚市|