2017-07-26 38 views
0

我想为yii使用3个模板。我有这样的文件:如何在Yii2中使用自定义布局?

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

我可以做些什么来应用所有的布局?因为我为我的网站使用了3个模板。在此先感谢

回答

1

只需简单地在控制器或控制器的动作

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

如果你想使用的布局中控制所有操作,

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

如果您要使用特定的动作布局比使用

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

如果你有基本的模板,你想使用你可以等,在配置/ web.php像

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

这将需要的

app/themes/mytheme/layout/main.php 

则布局中的功能,你可以使用其他模板一样

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