Detailed description of how to create and render views in the PHP Yii Framework. The Yii Framework view is part of the MVC mode. It is the code that shows data to end users. in web applications, the methods for creating views and rendering views are described in the Yii Framework of PHP. the yii Framework
View is part of the MVC pattern. It is the code used to display data to end users. in web applications, a view is created based on The View template. The View template is a PHP script file, which mainly contains HTML code and display PHP code, managed by yii \ web \ View application component. this component mainly provides common methods to help View construction and rendering. for simplicity, we call View templates or View template files as views.
Create View
As mentioned above, the view is a PHP script containing HTML and PHP code. the following code is a logon form View. you can see that the PHP code is used to generate dynamic content such as the page title and form, 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';?><?= Html::encode($this->title) ?>Please fill out the following fields to login:
<?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'username') ?> <?= $form->field($model, 'password')->passwordInput() ?> <?= Html::submitButton('Login') ?><?php ActiveForm::end(); ?>
In the View, you can access $ this to point to yii \ web \ View to manage and render this View file.
In addition to $ this, the view in the preceding example has other pre-defined variables, such as $ model, which represent the data transmitted to the view from the controller or other objects that trigger the view rendering.
Tip: pre-defined variables are listed in the comments of the view file header, which can be recognized by the IDE editor. this is also a good way to generate view documents.
Security
When creating a view that generates an HTML page, it is important to transcode and filter user input data before display. Otherwise, your application may be attacked by cross-site scripting.
To display plain text, call yii \ helpers \ Html: encode () for transcoding. for example, the following code transcodes the user name before Display:
<?phpuse yii\helpers\Html;?> <?= Html::encode($user->name) ?>
To display HTML content, call yii \ helpers \ HtmlPurifier to filter the content. for example, the following code filters the submitted content before Display:
<?phpuse yii\helpers\HtmlPurifier;?> <?= HtmlPurifier::process($post->text) ?>
Tip: HTMLPurifier does a good job in ensuring output data security, but its performance is poor. if your application needs high performance, consider caching filtered results.
Organization View
Similar to controllers and models, there are some conventions in the organization view:
The view file rendered by the controller is stored in the @ app/views/ControllerID directory by default. the ControllerID corresponds to the controller ID. for example, if the controller class is PostController, the view file directory should be @ app/views/post, the controller class PostCommentController corresponds to the Directory @ app/views/post-comment. if it is a controller in the Module, the directory should be yii \ base \ Module :: views/ControllerID directory under the basePath module directory;
By default, view files rendered by widgets are placed in the WidgetPath/views directory, where WidgetPath represents the directory where the widget class files are located;
For view files rendered by other objects, we recommend that you follow rules similar to those of widgets.
You can override the yii \ base \ ViewContextInterface: getViewPath () method of the controller or widget to customize the default directory of the view file.
Rendering View
You can call the rendering View method in the controller, widget, or other places to render the view. this method is similar to the following format:
/*** @ Param string $ view name or file path, determined by the actual rendering method * @ param array $ params the data passed to the view * @ return string rendering result */methodName ($ view, $ params = [])
Rendering in controller
In the controller, you can call the following controller method to render the view:
- Yii \ base \ Controller: render (): renders a View Name and returns the rendering result using a layout.
- Yii \ base \ Controller: renderPartial (): renders a View name without layout.
- Yii \ web \ Controller: renderAjax (): renders a View name without layout and injects all registered JS/CSS scripts and files, it is usually used in response to AJAX webpage requests.
- Yii \ base \ Controller: renderFile (): render 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 objects
A small object is an instance of CWidget or its subclass. it is a component mainly used to represent data. small objects are usually embedded in a view to generate complex and independent user interfaces. for example, a small calendar object can be used to render a complex calendar interface. small objects make the user interface more reusable.
We can use a small object as follows:
<? Php $ this-> beginWidget ('path. to. widgetclass');?>... Body of content that may be obtained 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. you can call CBaseController: beginWidget or CBaseController: widget to set its initialization attribute value. for example, when using a CMaskedTextField small object, we want to specify the mask to be used (which can be understood as an output format, note by the translator ). we implement this by passing an array containing the initialization values of these attributes. the key of the array is the name of the attribute, and the value of the array is the value corresponding to the attribute of the small object. as shown below:
<?php$this->widget('CMaskedTextField',array( 'mask'=>'99/99/9999'));?>
Inherit CWidget and overwrite its init () and run () methods to 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 () call }}
A small object can have its own view like a controller. by default, the view file of a small object is located under the views subdirectory that contains the directory of a small object file. these views can be rendered by calling CWidget: render (), which is similar to the controller. the only difference is that the views of small objects are not supported by layout files. In addition, $ this in the small object view points to the small object instance rather than the controller instance.
Rendering in View
You can render another View in the View. you can call the following methods provided by the yii \ base \ 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 webpage 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 directory where the View is located. remember that $ this corresponds to the yii \ base \ View component in the View:
<?= $this->render('_overview') ?>
Rendering elsewhere
You can access the Yii \ base \ view application component from anywhere by using the expression yii: $ app-> View, and call its method to render the view as described above, for example:
// Display the View File "@ app/views/site/license. php "echo \ Yii: $ app-> view-> renderFile ('@ app/views/site/license. php ');
Articles you may be interested in:
- Describes how to use the frontend resource package in the Yii Framework of PHP.
- Introduction to some advanced usage of cache in PHP Yii Framework
- In-depth analysis of the cache function in the PHP Yii Framework
- Advanced View usage in PHP Yii Framework
- PHP Yii Framework Model learning tutorial
- Detailed explanation of Controller in PHP Yii Framework
- How to remove the behavior bound to a component from the Yii Framework of PHP
- Definition and binding of behavior in PHP Yii Framework
- In-depth explanation of attributes in PHP Yii Framework)
- Detailed description of PHP Yii Framework extension installation and use
The timeline view is part of the MVC pattern. It is the code that shows data to end users. in web applications...