In Yii, Layout is a template for wrapping view content, which is used to maintain a consistent appearance and structure across multiple pages. It is a PHP file containing a complete HTML structure, usually contains tags such as ,
, , etc., and inserts dynamic content through = $content ?>. The default layout file is views/layouts/main.php. The controller sets the default layout through the $layout property. It can also overwrite the layout in the specific action, or even skip the layout. Layout files can be placed under views/layouts/ or in their subdirectories to differentiate between different modules. When using it, keep the layout simple and use partial views or widgets rationally to avoid too much logic embedded in it.

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.
The above is the detailed content of What are layouts in Yii, and how are they used?. For more information, please follow other related articles on the PHP Chinese website!