在Yii 中,Layout 是用於包裹視圖內(nèi)容的模板,用於保持多個頁面間的一致外觀和結(jié)構(gòu)。它是一個包含完整HTML 結(jié)構(gòu)的PHP 文件,通常包含、
、 等標(biāo)籤,並通過= $content ?> 插入動態(tài)內(nèi)容。默認(rèn)佈局文件是views/layouts/main.php,控制器通過$layout 屬性設(shè)置默認(rèn)佈局,也可以在具體action 中覆蓋佈局,甚至跳過佈局。佈局文件可放在views/layouts/ 下或其子目錄中,以實現(xiàn)不同模塊的區(qū)分。使用時應(yīng)保持佈局簡潔,合理利用partial views 或widgets,避免過多邏輯嵌入其中。

In Yii, layouts are templates used to wrap around the content of views. They help maintain a consistent look and structure across multiple pages—like having a common header, footer, or navigation bar without duplicating code.
What is a Layout in Yii?
A layout is basically a PHP file that contains the overall HTML structure of a page. It usually includes elements like
,
,
, and placeholders where the main content will be inserted.
The most common layout file in Yii applications is main.php
, which you'll typically find under views/layouts/
.
When a controller renders a view using $this->render()
, it automatically wraps that view inside the current layout unless told not to.
How to Use Layouts in Yii
Using layouts is straightforward. Here's how they work:
-
Set a default layout : You can define a default layout in your controller class by setting the $layout
property.
public $layout = 'main';
This tells all actions in that controller to use the main.php
layout unless overridden.
Override layout per action : If you want a specific action to use a different layout, set it before rendering:
public function actionLogin()
{
$this->layout = 'login'; // Uses views/layouts/login.php
return $this->render('login');
}
Use no layout at all : Sometimes, especially for AJAX responses or APIs, you don't need a layout. Just pass false
as the second argument to render()
:
return $this->render('contentOnly', null, false);
Accessing layout files : Layout files live in the views/layouts/
directory by default. You can also place them in subdirectories if needed, like views/layouts/admin/main.php
.
Structure of a Layout File
A layout file is just a regular PHP file with HTML and embedded PHP. The most important part is the placeholder for the content, which is rendered using <?= $content ?>
.
Here's a simplified example of what a layout might look like:
<!DOCTYPE html>
<html>
<head>
<title>My Yii App</title>
</head>
<body>
<header>
<?= $this->renderPartial('_nav') ?>
</header>
<main>
<?= $content ?>
</main>
<footer>
<p>© 2025 My Company</p>
</footer>
</body>
</html>
This layout includes a partial view _nav.php
for navigation and inserts the actual page content in the middle.
You can also include dynamic elements like alerts, breadcrumbs, or meta tags depending on the page being rendered.
Tips for Working with Layouts
- Keep your layouts clean: Don't put too much logic in them. Use partial views or widgets instead.
- Reuse layouts when possible: If several sections of your app have similar structures, consider sharing layouts between them.
- Customize based on context: You can conditionally load different styles or scripts within a layout based on the route or user role.
- Use layout hierarchies: For admin vs frontend layouts, create separate directories like
views/layouts/admin/
and views/layouts/site/
.
So, layouts in Yii are essential for maintaining a consistent design across your application. They let you define shared elements once and reuse them across multiple pages. With a little setup, you can switch between different layouts easily or even skip them when necessary.基本上就這些。
以上是YII中的佈局是什麼,如何使用?的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!