A detailed approach to creating views and rendering views in the PHP yii framework, the YII Framework _php Tutorial

Source: Internet
Author: User
Tags yii

A detailed approach to creating views and rendering views in the PHP yii framework, the YII framework


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.

Create 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 ';? >

<?= Html::encode ($this->title)?>

Please fill in 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, 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;? >  <?= html::encode ($user->name)?>

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;? >  <?= htmlpurifier::p rocess ($post->text)?>

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 ');

Articles you may be interested in:

    • A detailed description of the use of the front-end resource bundle in PHP's YII framework
    • Introduction to some advanced usage of caching in the YII framework of PHP
    • In-depth parsing of PHP's caching capabilities in the YII framework
    • The use of view view in the Yii framework of PHP is advanced
    • A tutorial on model models in the YII framework of PHP
    • Controller controllers in the YII framework of PHP
    • The method of removing the binding behavior of a component in PHP's YII framework
    • The definition and binding methods of behavior in the YII framework of PHP
    • In-depth explanation of properties in the Yii framework of PHP
    • A detailed description of the installation and use of PHP's YII framework Extension

http://www.bkjia.com/PHPjc/1117072.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117072.html techarticle The method of creating views and rendering views in the PHP Yii framework is explained in detail, and the Yii Framework view is part of the MVC pattern. It is the code that shows the data to the end user, in the Web application, according to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.