same layout for some pages in Yii2

yii2 layout templates
yii2 bootstrap 4 admin template

I am a newbie at yii . I have a page called main.php . I want some of the other pages to use main.php as layout. my project views are in "site" folder. I create a folder for that views and a separated controller. but it doesn't work.I add $this->layout = 'main' to my project

Put your main.php or any layout files in views/layouts folder.

Use this in controllers: public $layout = '/main';

Or in actions: $this->layout = '/main';

How to implement multiple page layouts in an application | Wiki, Designing Layouts ¶. To simplify the CSS design, we use the 960 Grid System. Because all layouts have the similar header and footer sections,  Sometimes you may want to nest one layout in another. For example, in different sections of a Web site, you want to use different layouts, while all these layouts share the same basic layout that generates the overall HTML5 page structure. You can achieve this goal by calling beginContent() and endContent() in the child layouts like the following:

In your Yii2 scaffolding should be there a directory


put the layout file main.php in this dir

(or edit or replace the existing one)

once you have done in the controllerAction where you need the new layout assign the layout

$this->layout = 'main';


class YourController extends Controller
    public function actionYourAction()

        $this->layout = 'main';
        return $this->render( ....        ]);

How to organize Design "things" in Yii 2 (themes and layout), some Images or other media. Lets call it "DesignAssets". What Yii needs: one or more Layout files; the view files; AssetBundle set in the layout  This extension consists of assets bundles, widgets, some page views and a layout example. Download Live Preview Get Hosting . 4. Yii2 Core. Yii2 Core was created as a fast start for building an advanced site based on Yii2. It covers typical use cases for a new project and will help you not to waste your time doing the same work in every project.

For changing for ALL controllers and actions you must add this in your config/main.php file:

     // ...
     'layout' => 'main',
     'components' => [

Change in one controller:

class SiteController extends Controller


    public $layout='//layouts/main';

    public function init() {
        // ...

Change just in one action:

public function actionIndex()
    $this->layout = 'mian';

    return $this->render('index', ['model' =>$model]);

same layout for some pages in Yii2, same layout for some pages in Yii2. yii2 layout templates yii2 render view inside view yii2 pass variable to layout yii2 document yii2 guide pdf. I am a newbie at  Yii - Layouts - Layouts represent the common parts of multiple views i.e. for example, page header and footer. By default, layouts should be stored in the views/layouts folder.

different layouts (or views) for different pages · Issue #6 · bupy7/yii2 , How set special layout for some static pages, and another layout for others? And use not only one view /default/index.php for all static pages. For example, the first Yii-based site I created used one header for the home page and another header for other pages, with all the remaining common elements being consistent. Or, I worked on an e-commerce project where one category of products used layout A and another category used layout B.

Yii2 By Example, layout background modifying, based on URL parameter 35 lazy loading 97 linked Yii2 10 modules creating 309, 310 multiple customers saving, at same time multiple grids displaying, on one page 148, 149 multiple layouts example 39,  However, the yii2-field-range extension has been enhanced to support horizontal form layout by default for \kartik\widgets\ActiveForm and allows you to easily define your markup for other form widgets based on yii\widgets\ActiveForm. Your options for a horizontal form layout are: Option 1 - Using \kartik\widgets\ActiveForm

Using multiple layouts - Yii2 By Example, During the building of a website or a web application, usually it could be required to render different views with different layouts. How set special layout for some static pages, and another layout for others? And use not only one view /default/index.php for all static pages.

  • update your question .. add the code for an controllerAction sample