Views are part of the MVC pattern. It is to display the data to the end user's code, in the Web application, according to the view template to create the view, the view template is a PHP script file, mainly contains HTML code and presentation class PHP code, through the Yii\web\view application component management, the component mainly provides a common method to help view construction and rendering, For simplicity, we call the view template or view template file a view.
As mentioned earlier, the view is a PHP script that contains HTML and PHP code, and the following code is a view of a login form, and you can see that the PHP code is used to generate dynamic content such as page titles and forms, and the HTML code organizes it into a beautiful HTML page.
<?phpuse Yii\helpers\html;use Yii\widgets\activeform; /* @var $this yii\web\view *//* @var $form yii\widgets\activeform *//* @var $model app\models\loginform * * $this->title = ' Login ';? >
In the view, accessible $this point to Yii\web\view to manage and render this view file.
In addition to $this, the views in the example above have other predefined variables, such as $model, that represent the data that is passed into the view from the controller or from other triggering views of the rendered object.
Tip: The predefined variables are listed in the header of the view file, which can be recognized by the IDE editor and is a good way to generate a viewing document.
Safety
When creating a view that generates an HTML page, it is important to transcode and filter user input data before it is displayed, otherwise your app may be attacked by cross-site scripting.
To display plain text, first call Yii\helpers\html::encode () for transcoding, such as the following code to transcode the user name before it is displayed:
<?phpuse yii\helpers\html;? > <div class= "username" > <?= html::encode ($user->name)?></div>
To display HTML content, first call the Yii\helpers\htmlpurifier filter content, such as the following code to filter the submission before the display:
<?phpuse yii\helpers\htmlpurifier;? > <div class= "POST" > <?= htmlpurifier::p rocess ($post->text)?></div>
Tip: Htmlpurifier is doing a good job of securing output data, but performance is poor, and if your application needs high performance consider caching-filtered results.
Organization View
Similar to controllers and models, there are some conventions on the organization view:
The view file rendered by the controller is placed by default in the @app/views/controllerid directory, where Controllerid corresponds to the controller ID, for example, the controller class is Postcontroller, and the view file directory should be @app/views/post. Controller class Postcommentcontroller The corresponding directory is @app/views/post-comment, if the controller in the module, the directory should be yii\base\module::basepath module directory views/ Controllerid directory;
The view file rendered for the widget is placed by default in the Widgetpath/views directory, where Widgetpath represents the directory where the widget class file resides;
For view files rendered by other objects, it is recommended that you follow the rules similar to widgets.
You can override the Yii\base\viewcontextinterface::getviewpath () method of the controller or widget to customize the view file default directory.
Render View
The render view method can be invoked in the controller, widget, or elsewhere to render the view, which resembles the following format:
/** * @param string $view view name or file path, determined by the actual rendering method * @param array $params The data passed to the view * @return the string rendering result */methodname ($view, $p Arams = [])
Rendering in Controller
In the controller, the following controller methods can be called to render the view:
Yii\base\controller::render (): Renders a view name and uses a layout to return to the rendered result.
Yii\base\controller::renderpartial (): Renders a view name and does not use a layout.
Yii\web\controller::renderajax (): Renders a view name and does not use layouts, and injects all registered JS/CSS scripts and files, usually in response to an AJAX Web request.
Yii\base\controller::renderfile (): Renders a view file under a view file directory or alias.
For example:
namespace App\controllers; Use Yii;use app\models\post;use yii\web\controller;use yii\web\notfoundhttpexception; Class Postcontroller extends controller{public function Actionview ($id) { $model = Post::findone ($id); if ($model = = = null) { throw new notfoundhttpexception; } Render a view named "View" and use the layout return $this->render (' View ', [ ' model ' = ' $model, ])} }
Small items
A small object is an instance of CWidget or its subclasses. It is a component that is primarily used to represent data. Small objects are often embedded in a single view to create a complex and independent user interface. For example, a calendar widget can be used to render a complex calendar interface. Small objects make the user interface more reusable.
We can use a small object according to the following view script:
<?php $this->beginwidget (' Path.to.WidgetClass '); Content bodies that may be acquired by small objects ... <?php $this->endwidget ();?>
Or
<?php $this->widget (' Path.to.WidgetClass ');?>
The latter is used for components that do not require any body content.
A small object can be configured to customize its performance. This is done by calling Cbasecontroller::beginwidget or Cbasecontroller::widget to set its initialization property value. For example, when using the Cmaskedtextfield Small objects, we want to specify the mask used (can be understood as an output format, the translator Note). We do this by passing an array that carries the initialization values for these properties. The key to the array here is the name of the property, and the value of the array is the value of the small object property. As shown below:
<?php$this->widget (' Cmaskedtextfield ', Array ( ' mask ' = ' 99/99/9999 '));? >
Inheriting CWidget and overwriting its init () and run () methods, you can define a new small object:
Class Mywidget extends cwidget{public function init () { //This method is called by Ccontroller::beginwidget () } Public function run () { //This method will be called by Ccontroller::endwidget () }}
A small object can have its own view like a controller. By default, the view file for small objects is located under the Views subdirectory that contains the file directory of the Small object class. These views can be rendered by calling Cwidget::render (), which is similar to the controller. The only difference is that The view of small objects does not have layout file support. In addition, $this in the Small object view point to the small object instance instead of the controller instance.
Rendering in views
You can render another view in the view, and you can invoke the following methods provided by the Yii\base\view View component:
Yii\base\view::render (): Renders a view name.
Yii\web\view::renderajax (): Renders a view name and injects all registered JS/CSS scripts and files, typically in response to an AJAX Web request.
Yii\base\view::renderfile (): Renders a view file under a view file directory or alias.
For example, the following code in the view renders the _overview.php view file in the same directory as the view, remembering that the corresponding Yii\base\view component is $this in the view:
<?= $this->render (' _overview ')?>
Other places to render
Can be used anywhere through the expression Yii:: $app->view accesses the Yii\base\view application component, invokes it to render the view as described in the previous method, for example:
Show view File "@app/views/site/license.php" echo \yii:: $app->view->renderfile (' @app/views/site/license.php ');