Yii Framework Official Guide Series 9 - Basics: Views
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 |
|
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 |
|
$content stores the content The rendering result of the view.
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() .
1 2 3 |
|
1 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
. 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
, 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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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

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

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 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

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

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
