Views are part of the MVC pattern. It is the display data to end user's code, in the Web application, according to the view template to create the view, the view template is the php script file, mainly contains the HTML code and the demonstration class PHP code, manages through the Yii\web\view application component, this component mainly provides the common method to help the view constructs and renders, For simplicity, we call the view template or view template file a view.
Create a View
As mentioned earlier, the view is a PHP script that contains HTML and PHP code, 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.
<?php use
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, an accessible $this points to Yii\web\view to manage and render this view file.
In addition to $this, the views in the preceding example have other predefined variables, such as $model, that represent the data that is passed into the view from the controller or other object that triggers the view.
Tip: Column predefined variables to the view file header annotation, which can be recognized by the IDE editor and is a good way to generate a visual document.
Safety
When creating a view that generates an HTML page, it is important that the user input data be transcoding and filtered before it is displayed, otherwise your application may be attacked by Cross-site scripting.
To display plain text, first invoke Yii\helpers\html::encode () for transcoding, such as the following code to turn the username before it is displayed:
<?php use
yii\helpers\html;
? >
<div class= "username" >
<?= html::encode ($user->name)?>
</div>
To display HTML content, first invoke the Yii\helpers\htmlpurifier filter content, such as the following code to filter the submission before display:
<?php use
yii\helpers\htmlpurifier;
? >
<div class= "POST" >
<?= htmlpurifier::p rocess ($post->text)?>
</div>
Tip: Htmlpurifier is good at guaranteeing output data security, but poor performance, if your application requires high performance to consider the results of caching filtering.
Organize views
Similar to the controller and model, there are some conventions in the Organization view:
The view file rendered by the controller is placed by default in the @app/views/controllerid directory, where Controllerid the corresponding controller ID, for example, the controller class is Postcontroller, and the view file directory should be @app/views/post. Controller class Postcommentcontroller corresponding directory for @app/views/post-comment, if the controller in the module, the directory should be Yii\base\module::basepath module directory of views/ Controllerid directory;
The view file for widget rendering defaults to 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 rules that are similar to widgets.
The Yii\base\viewcontextinterface::getviewpath () method that overrides the controller or widget is customized to customize the view file default directory.
Render View
The rendering view method can be invoked in the controller, widget, or elsewhere to render the view, similar to 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 string render results
*/
methodname ($view, $params = [])
Render in Controller
In the controller, you can invoke the following controller methods to render the view:
- Yii\base\controller::render (): Renders a view name and returns it to the render result using a layout.
- 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 layout, and injects all registered JS/CSS scripts and files, usually in response to AJAX Web requests.
- 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;
}
Renders a view named "View" and uses the layout return
$this->render (' View ', [
' model ' => $model,
]
}
}
Small objects
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 view to produce complex and independent user interfaces. 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 in the following view script:
<?php $this->beginwidget (' Path.to.WidgetClass '),?>
... Content bodies that may be fetched 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.
Small objects can be configured to customize their performance. This is done by calling Cbasecontroller::beginwidget or Cbasecontroller::widget to set its initialization property value. For example, when using the Cmaskedtextfield Small object, we want to specify the mask used (which can be interpreted as an output format, we do this by passing an array that carries the initialization values of these attributes. The key of 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 '
));
? >
To inherit CWidget and overwrite its init () and run () methods, you can define a new small object:
Class Mywidget extends CWidget
{public
function init ()
{
//This method will be 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 the widget is located under the Views subdirectory that contains the small object class file directory. These views can be rendered by calling Cwidget::render (), which is similar to the controller. The only difference is that A view of a small object has no layout file support. In addition, the $this in the widget view points to a small object instance instead of a controller instance.
Rendering in view
You can render another view in a 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, usually in response to AJAX Web requests.
- 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 $this corresponding Yii\base\view components in the view:
<?= $this->render (' _overview ')?>
Other places rendering
You can use the expression Yii anywhere:: $app->view access to the Yii\base\view application component, and invoke 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 ' );