views are responsible for presenting the data to end users. in web applications, views are just php script files containing html and php code.
creating views
step 1 − let us have a look at the ‘about’ view of the basic application template.
<?php
   /* @var $this yii\web\view */
   use yii\helpers\html;
   $this->title = 'about';
   $this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
   <h1><?= html::encode($this->title) ?></h1>
   <p>
      this is the about page. you may modify the following file to customize its content:
   </p>
   <code><?= __file__ ?></code>
</div>
the $this variable refers to the view component that manages and renders this view template.
this is how the ‘about’ page looks like −
it is important to encode and/or filter the data coming from the end user in order to avoid the xss attacks. you should always encode a plain text by calling yii\helpers\html::encode() and html content by calling yii\helpers\htmlpurifier.
step 2 − modify the ‘about’ view in the following way.
<?php
   /* @var $this yii\web\view */
   use yii\helpers\html;
   use yii\helpers\htmlpurifier;
   $this->title = 'about';
   $this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
   <h1><?= html::encode($this->title) ?></h1>
   <p>
      this is the about page. you may modify the following file to customize its content:
   </p>
   <p>
      <?= html::encode("<script>alert('alert!');</script><h1>encode example</h1>>") ?>
   </p>
   <p>
      <?= htmlpurifier::process("<script>alert('alert!');</script><h1> htmlpurifier example</h1>") ?>
   </p>
   <code><?= __file__ ?></code>
</div>
step 3 − now type http://localhost:8080/index.php?r=site/about. you will see the following screen.
notice, that the javascript code inside the html::encode() function is displayed as plain text. the same thing is for htmlpurifier::process() call. only h1 tag is being displayed.
views follow these conventions −
views, which are rendered by a controller, should be put into the @app/views/controllerid folder.
views, which are rendered in a widget, should be put into the widgetpath/views folder.
to render a view within a controller, you may use the following methods −
render() − renders a view and applies a layout.
renderpartial() − renders a view without a layout.
renderajax() − renders a view without a layout, but injects all registered js and css files.
renderfile() − renders a view in a given file path or alias.
rendercontent() − renders a static string and applies a layout.
to render a view within another view, you may use the following methods −
render() − renders a view.
renderajax() − renders a view without a layout, but injects all registered js and css files.
renderfile() − renders a view in a given file path or alias.
step 4 − inside the views/site folder, create two view files: _part1.php and _part2.php.
_part1.php −
<h1>part 1</h1>
_part2.php −
<h1>part 2</h1>
step 5 − finally, render these two newly created views inside the ‘about’ view.
<?php
   /* @var $this yii\web\view */
   use yii\helpers\html;
   $this->title = 'about';
   $this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
   <h1><?= html::encode($this->title) ?></h1>
   <p>
      this is the about page. you may modify the following file to customize its content:
   </p>
   <?= $this->render("_part1") ?>
   <?= $this->render("_part2") ?>
   <code><?= __file__ ?></code>
</div>
you will see the following output −
when rendering a view, you can define the view using as a view name or a view file path/alias. a view name is resolved in the following way −
a view name can omit the extension. for example, the about view corresponds to the about.php file.
if the view name starts with “/”, then if currently active module is forum, and the view name is comment/post, the path would be @app/modules/forum/views/comment/post. if there is no active module, the path would be @app/views/comment/post.
if the view name starts with “//”, the corresponding path would be @app/views/viewname. for example, //site/contact corresponds to @app/views/site/contact.php.
if the view name is contact, and the context controller is sitecontroller, then the path would be @app/views/site/contact.php.
if the price view is rendered within the goods view, then price would be resolved as @app/views/invoice/price.php if it is being rendered in the @app/views/invoice/goods.php.
accessing data in views
to access data within a view, you should pass the data as the second parameter to the view rendering method.
step 1 − modify the actionabout of the sitecontroller.
public function actionabout() {
   $email = "admin@support.com";
   $phone = "+78007898100";
   return $this->render('about',[
      'email' => $email,
      'phone' => $phone
   ]);
}
in the code given above, we pass two variables $email and $phone to render in the about view.
step 2 − change the about view code.
<?php
   /* @var $this yii\web\view */
   use yii\helpers\html;
   $this->title = 'about';
   $this->params['breadcrumbs'][] = $this->title;
?>
<div class = "site-about">
   <h1><?= html::encode($this->title) ?></h1>
   <p>
      this is the about page. you may modify the following file to customize its content:
   </p>
   <p>
      <b>email:</b> <?= $email ?>
   </p>
   <p>
      <b>phone:</b> <?= $phone ?>
   </p>
   <code><?= __file__ ?></code>
</div>
we have just added two variables that we received from the sitecontroller.
step 3 − type the url http://localhost:8080/index.php?r=site/about in the web browser, you will see the following.