Yii Framework official guide series 9-Basic Knowledge: View

Source: Internet
Author: User
A view is a PHP script that contains the main user interaction elements. it can contain PHP statements, but we recommend that these statements do not change the data model, and it is best to keep it simple (simply as a view ). To implement logic and interface analysis...



A view is a PHP script that contains the main user interaction elements. it can contain PHP statements, but we recommend that these statements do not change the data model, and it is best to keep it simple (simply as a view ). To separate the logic from the interface, the logic of the large segment should be placed in the controller or model, rather than in the view.

A view has a name. when rendering (render), the name is used to identify the View script file. The View name is the same as the View script name. for example: VieweditThe name is from a script file named edit. php. if you want to render the file, you need to call CController: render () by passing the View name (). This method will find the corresponding view file in the protected/views/ControllerID directory.

In the view script, we can use $ this to access the controller instance. in the view, we can use $ this-> propertyName or take any attributes of the controller.

We can also use the following push method to transmit data to the view:


$this->render('edit', array(    'var1'=>$value1,    'var2'=>$value2,));

In the preceding method, the render () method extracts the second parameter of the array into the variable. the result is that in the view script, we can directly access the variables $ var1 and $ var2.

1. Layout

A layout is a special view file used to modify a view. it usually contains some common views on the user interface. for example, the layout can contain the header and footer parts, and then embed the content in the layout.


......header here......
 ......footer here......

The$contentThe rendering result of the content View is stored.

When render () is used, the layout is implicitly applied. View scriptprotected/views/layouts/main.phpIs the default layout file, which can be customized by changing CWebApplication: layout or CWebApplication: layout. To render a view without layout, call renderPartial ().

2. 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:


 BeginWidget ('path. to. widgetclass');?>... Body of content that may be obtained by small objects...
 EndWidget ();?>

Or


 widget('path.to.WidgetClass'); ?>

The latter is used for components that do not require any body content.

You can configure a small object to customize its performance. this is done by calling CBaseController: beginWidget or CBaseController: widget to set its initialization attribute value. For example, when using a CMaskedTextField small object, we want to specify the used mask (which can be understood as an output format), which is implemented by passing an array carrying 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:


 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 in a directory containing a small object file.viewsSubdirectory. 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, in the small object view$thisPoint to a small object instance rather than a controller instance.

3. system View

Rendering of the system view is usually used to display errors and log information of Yii. For example, when a user requests a controller or action that does not exist, Yii throws an exception to explain this error. in this case, Yii uses a special system view to display this error.

The system View naming rules follow. For exampleerrorXXXThis name is used to render the display error number.XXXView of CHttpException. For example, if CHttpException throws a 404 errorerror404It will be displayed.

Inframework/viewsYii provides a series of default system views. They canprotected/views/systemCreate a view file with the same name for customization.

The above is the Yii Framework official guide series 9-Basic Knowledge: View content. For more information, see PHP Chinese network (www.php1.cn )!

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.