Table of Contents
1. Layout
The rendering of system view is usually used to display Yii errors and log information. For example, when a user request comes from a controller or action that does not exist, Yii will throw an exception to explain the error. At this time, Yii will use a special system view to display the error.
Home Backend Development PHP Tutorial Yii Framework Official Guide Series 9 - Basics: Views

Yii Framework Official Guide Series 9 - Basics: Views

Feb 11, 2017 am 09:38 AM



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 (only as a view). In order to achieve separation of logic and interface, large sections of logic should be placed in the controller or model rather than in the view.

The view has a name, and when rendering (render), the name will be used to identify the view script file. The name of the view is the same as the name of its view script. For example: the name of the view edit comes from a script file named edit.php. To render, you need to call CController::render by passing the name of the view. (). This method will search for the corresponding view file in the protected/views/ControllerID directory.

Inside the view script, we can access the controller instance through $this. We can use $this-> propertyName or get any property of the controller.

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


1

2

3

4

$this->render('edit'array(

    'var1'=>$value1,

    'var2'=>$value2,

));

Copy after login

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

1. Layout

Layout is a special view file used to modify the view. It usually contains a common part of the view in the user interface. For example: the layout can include header and footer part, and then embed the content within it.


##

1

2

3

......header here......

<?php echo $content; ?>

......footer here......

Copy after login

$content stores the content The rendering result of the view.

When using render(), the layout is implicitly applied. The view script

protected/views/layouts/main.php is the default layout file. This can be customized by changing CWebApplication::layout or CWebApplication::layout. To render a view without layout, you need to call renderPartial() .

2. Widget

A widget is an instance of CWidget or its subclass. It is a component mainly used to represent data. Widgets are often embedded in a view to produce complex and independent user interfaces. For example, a calendar widget can be used to render a complex calendar interface. Gizmos make user interfaces more reusable.

We can use a small object according to the following view script:


1

2

3

<?php $this->beginWidget('path.to.WidgetClass'); ?>

...可能会由小物件获取的内容主体...

<?php $this->endWidget(); ?>

Copy after login

or


1

<?php $this->widget('path.to.WidgetClass'); ?>

Copy after login

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

Widgets can customize their performance through configuration. This is done by calling CBaseController::beginWidget or CBaseController::widget to set its initialization property value. For example, when using the CMaskedTextField widget, we want to specify the mask to be used (which can be understood as an output format). We do this by passing an array carrying the initialization values ​​​​of these properties. The key of the array here is the name of the attribute, and the value of the array is the value corresponding to the small object attribute. As shown below:


1

2

3

4

5

<?php

$this->widget('CMaskedTextField',array(

    'mask'=>'99/99/9999'

));

?>

Copy after login

Inherit CWidget and override its init() and run() methods, you can define a New widgets:


1

2

3

4

5

6

7

8

9

10

11

12

class MyWidget extends CWidget

{

    public function init()

    {

        // 此方法会被 CController::beginWidget() 调用

    }

 

    public function run()

    {

        // 此方法会被 CController::endWidget() 调用

    }

}

Copy after login

##A widget can have its own view just like a controller

. By default, the view files of small objects are located under the views subdirectory of the directory containing the small object class files. These views can be rendered by calling CWidget::render(), much like controllers. The only difference is that the widget's view does not have layout file support. In addition, $this in the widget view points to the widget instance rather than the controller instance. 3. System view

The rendering of system view is usually used to display Yii errors and log information. For example, when a user request comes from a controller or action that does not exist, Yii will throw an exception to explain the error. At this time, Yii will use a special system view to display the error.

The naming of system views follows some rules. For example, a name like

errorXXX

is used to render the CHttpException view showing the error number XXX. For example, if CHttpException throws a 404 error, then error404 will be displayed. Under

framework/views

, Yii provides a series of default system views. They can be customized by creating a view file with the same name under protected/views/system. The above is the Yii Framework Official Guide Series 9 - Basic Knowledge: View Contents. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement editable tables in Vue How to implement editable tables in Vue Nov 08, 2023 pm 12:51 PM

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

Understand the differences and comparisons between SpringBoot and SpringMVC Understand the differences and comparisons between SpringBoot and SpringMVC Dec 29, 2023 am 09:20 AM

Compare SpringBoot and SpringMVC and understand their differences. With the continuous development of Java development, the Spring framework has become the first choice for many developers and enterprises. In the Spring ecosystem, SpringBoot and SpringMVC are two very important components. Although they are both based on the Spring framework, there are some differences in functions and usage. This article will focus on comparing SpringBoot and Spring

What are the views in Word? What are the views in Word? Mar 19, 2024 pm 06:10 PM

I guess that many students want to learn the typesetting skills of Word, but the editor secretly tells you that before learning the typesetting skills, you need to understand the word views clearly. In Word2007, 5 views are provided for users to choose. These 5 views include pages. View, reading layout view, web layout view, outline view and normal view, let’s learn about these 5 word views with the editor today. 1. Page view Page view can display the appearance of the print result of the Word2007 document, which mainly includes headers, footers, graphic objects, column settings, page margins and other elements. It is the page view closest to the print result. 2. Reading layout view Reading layout view displays Word2007 documents and Office in the column style of a book

Yii framework middleware: providing multiple data storage support for applications Yii framework middleware: providing multiple data storage support for applications Jul 28, 2023 pm 12:43 PM

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

How to use Yii framework in PHP How to use Yii framework in PHP Jun 27, 2023 pm 07:00 PM

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using Yii framework Steps to implement web page caching and page chunking using Yii framework Jul 30, 2023 am 09:22 AM

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

Yii Framework Middleware: Add logging and debugging capabilities to your application Yii Framework Middleware: Add logging and debugging capabilities to your application Jul 28, 2023 pm 08:49 PM

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

How to use controllers to handle Ajax requests in the Yii framework How to use controllers to handle Ajax requests in the Yii framework Jul 28, 2023 pm 07:37 PM

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

See all articles