Home>Article>PHP Framework> Views in the Yii framework: building web interfaces

Views in the Yii framework: building web interfaces

王林
王林 Original
2023-06-21 12:50:25 827browse

Yii framework is a popular PHP development framework that is commonly used in web applications to develop and manage websites. The MVC architecture of the Yii framework is a key feature of it, in which views are often used to build web interfaces. In this article, we will introduce views in Yii framework and their usage in detail.

1. View types

In the Yii framework, there are two types of views: traditional views and fragment views. A traditional view is the complete HTML structure of the page, a fragment view is just a partial view with some functionality. For example, in a shopping cart application, you might want to use a fragment view to display the contents of the shopping cart, rather than using a traditional view of the entire page.

2. Structure of view files

View files have a .php file extension and are saved in the /views directory of the project. View files usually contain HTML code and PHP scripts. In the Yii framework, the naming rules of view files are named according to the relevant controllers and operations. For example, if the controller is named SiteController and the operation is named login, then the corresponding view file is named site/login.php.

3. Data transfer in views

In the Yii framework, data in the controller can be transferred to the corresponding view. Generally speaking, data can be passed in the following two ways:

1. Use the render method to pass data

Usually, variables are defined in the controller's action and passed to the view file, Data can be passed using the "render" method of the Yii framework. For example, the following code demonstrates how to pass the $data variable to the corresponding view file:

public function actionIndex()
{

$data = "Welcome to Yii!"; return $this->render('index', [ 'data' => $data, ]);

}

In the corresponding view file (e.g. views/site/index.php), the passed data can be used like this:

c1a436a314ed609750bd7c7d319db4da9be93207f8432b8a30f6e17990eff85e2e9b454fa8428549ca2e64dfac4625cd

2. Use components in the view file to pass data

In the view file, the Yii framework provides some special components (such as Yii::$app and $this) to access data in the controller. The following code demonstrates how to use variables defined in the controller:

c1a436a314ed609750bd7c7d319db4daa7c11fa571097ef5b17f1505b9745517context->pageTitle ?>2e9b454fa8428549ca2e64dfac4625cd

In this example, $this represents the view itself, $this->context represents the controller object, and $pageTitle is a variable in the controller.

4. Formatting data in views

Data in view files usually need to be formatted. In the Yii framework, you can use Yii's formatting class to complete these tasks. For example, the following code demonstrates how to format dates and times using Yii's Yii::$app->formatter class:

83e0b9f8cdad4f031f9e4efdaa38d325formatter->asDate($ date) ?>
83e0b9f8cdad4f031f9e4efdaa38d325formatter->asTime($time) ?>

$date and $time in brackets are passed to Variables in view files.

5. Dynamic content in view files

In view files, you can embed any PHP code snippets and HTML tags. The following code demonstrates how to embed PHP code and HTML markup in a view file:

ba76784c2e6560b2f8abaf9a0e33fc5d

Success!

5f0317c1e16c52e226e76d65c7f9596d

Failure!

4172bdfc6e8dc1b1be839b7983ed4f73

In this example, condition is a variable passed to the view file.

6. Widgets in view files

In the Yii framework, a widget (Widget) is a reusable code block that can be embedded anywhere in the view file. The following code demonstrates how to use widgets in a view file:

c87f68c6068bb0e46eef6615e56875fc

In this example , the Menu widget generates a navigation menu. The widget uses an array to define the menu items and specifies the label and URL of each menu item. Note that to use widgets, you need to add the corresponding widget class first.

7. Layout in the view

In the Yii framework, layout is a template file that defines the overall structure of the website and the location of its various parts. In the view file, you can use the layout file to define the structure and style of the entire page. The following code demonstrates how to use layout files:

26a80da47b56f4831cdf7ae7bb070476beginContent('@app/views/layouts/main.php'); ?>

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

This code contains the beginContent() and endContent() methods, which are special functions used to define layout files. In this example, the "@app/views/layouts/main.php" layout file is used. $content is a variable in the view file that serves as the main page content, and is referenced in the layout file in the form of bf0fb082246d4908f10891e5c4c12826.

8. Summary

In the Yii framework, views are an important part of building a web interface. In this article, we introduced the view types in the Yii framework, the structure of the view file, the transfer of data in the view, the data formatting in the view, the dynamic content in the view file, the widgets in the view file, and the layout. Hope this article helps you better understand views in Yii framework and their usage.

The above is the detailed content of Views in the Yii framework: building web interfaces. For more information, please follow other related articles on the PHP Chinese website!

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