Yii Framework Analysis (9)-View rendering process

Source: Internet
Author: User
Yii Framework Analysis (9)-View rendering process

In the MVC architecture, View is mainly used to display information. The view layer file in Yii consists of two parts: Layout view and partial view. Most pages of the web system have the same elements: logo, menu, foot bar, etc. We call these View files composed of the same elements a layout view, generally, the web system requires two la S, namely, the foreground layout and the background layout. The foreground layout is for the user, and the background layout is for the administrator. Some views that are unique to each page are called partial views.

You can describe it as needed. We put the menu bar, navigation bar, and Footer bar into the layout file, that is, all pages reuse a layout file, and each page (Action) has its own part of the view file.
Next, let's take a look at the storage path of the view file. WebApp can configure the view file path and layout file path, and also specify a default layout file. The View file of each Controller is stored under the View path specified by WebApp, with the postfix of the position in the Controller name, the Controller can also specify the layout file used by the Controller.
WebApp member attributes
ViewPath is used to specify the view file path. all view files must be under this file
Default protected/views
LayoutPath is used to specify the layout file path. all layout files must be under this file
The default value is protected/views/layouts, which includes main. php and column. php.
ViewPath is used to specify the path of the system view file. the default value is protected/views/system.
Layout specifies the layout file used by default. the default value is main.

For example, if PostController modifyAction is being executed and PostController specifies to use the column layout, the layout file used by this request is protected/views/layouts/column. php. The View file is protected/views/post/modify. php.
There are two important concepts in the view layer: client script components and widgets.
Client script component: This component is used to manage client scripts (javascript and css). you can use this component to add javascript and css to the view. the client script component manages the code in a unified manner, the final step of page output is to render the client script (javascript and css.
Widget: It is also called a small object. you can use widgets to modularize the page. widgets can be viewed as a non-layout controller. Widgets can be used to reuse common page elements, such as Menu widgets, list widgets, table widgets, and paging widgets.

The rendering of the view layer is completed in three steps:
Step 1: render part views, that is, rendering the view fragments specific to each page;
Step 2: render the Layout view, that is, rendering the page elements shared by each page, and insert the result of step 1 into the layout view. In step 1 and Step 2, you may also need to render widgets, such as Calendar widgets and menu widgets. In these two steps, you can register the js and css you have used;
Step 3: render js and css. Add the js and css registered in the previous two steps to the specified location on the html page.

Due to the loading order, the result is that the css file or js file imported using registerCssFile or registerScriptFile in some views will appear before the css or js file registered in the layout view file, how can we solve this problem when page styles and dynamic effects are disordered?

In fact, it is very simple, that is, re-write the init method in protected/components/Controller. php, and define the method for registering a global js or css file in the layout view in the init method:

public function init(){    Yii::app()->clientScript->registerCoreScript('jquery');    Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . '/css/style.css');    parent::init();}

This ensures that the jquery.jsand style.css files are defined in the head tag before all other js and css files.

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.