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:
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:
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:
= Yii::$app->formatter->asDate($ date) ?>
= Yii::$app->formatter->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:
<div class="alert alert-success">Success!</div>
<div class="alert alert-danger">Failure!</div>
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:
= yiiwidgetsMenu::widget([
'items' => [ ['label' => 'Home', 'url' => ['/site/index']], ['label' => 'About', 'url' => ['/site/about']], ['label' => 'Contact', 'url' => ['/site/contact']], ],
]) ?>
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:
beginContent('@app/views/layouts/main.php'); ?>
<div class="container"> <?= $content ?> </div>
< ;?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 = $content ?>.
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!