简体   繁体   中英

How to use custom layout in Yii2?

I want to use 3 templates for yii. I have files like these:

./views/layouts/main-template-1.php
./views/layouts/main-template-2.php
./views/layouts/main-template-3.php

What may I do to apply all of the layouts? Because I use 3 templates for my web. Thanks in advance

If you want to use layout for all actions in Controller ,

class SiteController extends Controller
{
    public $layout="main-template-1";
     // actions
}

If you want to use layout for specific action than use

public function actionIndex()
{
$this->layout = "main-template-1"; 
}

只需放置在控制器或控制器的动作中

$this->layout = 'main-template-1'; // or 2 or 3

If you have basic template that you want to use you can set that in config/web.php like

'view' => [
        'theme' => [
            'pathMap' => [
                    '@app/views' =>[
                            '@app/themes/mytheme',
                            '@app/themes/yourtheme'
                            ]
                        ],
            'baseUrl' => '@web/../themes/mytheme',
        ],
    ],

this will take the layout of

app/themes/mytheme/layout/main.php

then in functions you could use other templates like

public function actionIndex()
{
   $this->layout = "main-template-1"; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM