Yii Framework Analysis (ix)--View rendering process

Source: Internet
Author: User

In the MVC architecture, view is primarily used to display information. The view layer file in Yii consists of 2 parts: Layout view, partial view. Most pages of the web system have the same elements: logos, menus, foot columns, etc., we put these same elements of the view file is called Layout view, the general web system needs 2 layouts, that is, the foreground layout and background layout, the front layout is for the user to see, the background layout is for the administrator to see. Some views that are unique to each page are called partial views.

Can be described using, we put the menu bar, navigation bar and footer bar in the layout file, that is, all the pages are reused a layout file, and then each page (Action) has its own partial view file.
Next, take a look at the storage path for the view file. WebApp can configure the view file path and layout file path and also specify a default layout file, each controller's view file is placed under the WebApp specified view path, with the controller's name position suffix, The controller can also specify which layout file to use.
WebApp member Property Description
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 protected/views/layouts, which is under the path: main.php, column.php
ViewPath is used to specify the system view file path, the default Protected/views/system
Layout Specifies the default use of the layouts file, which defaults to main

For example, the Modifyaction,postcontroller that is currently executing postcontroller specifies that the column layout is used, so the layout file used by this request is protected/views/layouts/ column.php, the view file is protected/views/post/modify.php.
There are also 2 important concepts in the view layer: The client Script component, the Widget.
Client Script Component: This component is used to manage client script (JavaScript and CSS), where you can add JavaScript and CSS to the view, and the client Script component manages the code uniformly, in the final step of the page output to the client script ( JavaScript and CSS) for rendering.
Widgets: Also known as small objects, widgets can be used to block the page, widgets can be seen as a controller without layout. Widgets allow you to reuse common page elements, such as Menu widgets, List widgets, form widgets, pagination widgets, and more.

The rendering of the view layer is done in 3 steps:
STEP1: Render part of the view, that is, each page renders each of its own unique visual image broken;
STEP2: Renders the layout view, which renders the page elements that are common to each page, and inserts the results of the STEP1 into the layout view. In Step1 and STEP2, you may also need to render widgets, such as Calendar widgets, menu widgets, and so on. These 2 steps can register their own use of which JS and CSS;
STEP3: Render JS and CSS. Add the first 2 steps of JS and CSS to the location where the HTML page is set up.

Because of the loading order, the result is that using Registercssfile or registerscriptfile imported CSS files or JS files in a partial view will appear before the CSS or JS files that are registered in the Layout view file. Cause the page style and the dynamic effect confusion, how solves this question?

In fact, it is very simple to rewrite the Init method in protected/components/controller.php, and define the method of registering the 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 will ensure that the jquery.js and STYLE.CSS files are defined in the head tag before all other JS and CSS files.

Yii Framework Analysis (ix)--View rendering process

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.