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';
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.161.116