Yii2如何将布局文件拆分为单独的页眉和页脚?

Sam*_*ura 14 php yii2

我是新手YII2,所以这可能是一个非常基本的问题.

我已经设置了初始申请.我为我的YII2应用程序设置了主题/themes/standard

现在,有一个默认的布局文件themes/standard/layouts/main.php- 它有页眉和页脚的html代码

我想将标题代码分隔themes/standard/layouts/header.php为另一个文件和页脚

我尝试过类似下面的代码 main.php

<?php $this->render("header"); ?>
Run Code Online (Sandbox Code Playgroud)

也试过这个

<?php $this->render("//layouts/header"); ?>
Run Code Online (Sandbox Code Playgroud)

但它没有呈现内容.因为我有主题,所以我不想绝对的路径你们可以帮助这个吗?

Ali*_*our 12

为了有Nested Layouts,您可以使用beginContent()endContent()下面一样(在你main.php的布局为例):

<?php $this->beginContent('@app/views/layouts/header.php'); ?>
    <!-- You may need to put some content here -->
<?php $this->endContent(); ?>
Run Code Online (Sandbox Code Playgroud)

介于两者之间的begin,并end会被替换机智$contentheader.php.


截至Yii2官方的例子:

有时您可能希望将一个布局嵌套在另一个布局中.例如,在Web站点的不同部分中,您希望使用不同的布局,而所有这些布局共享生成整个HTML5页面结构的相同基本布局.您可以通过调用beginContent()以及endContent()子布局来实现此目标,如下所示:

<?php $this->beginContent('@app/views/layouts/base.php'); ?>

...child layout content here...

<?php $this->endContent(); ?>
Run Code Online (Sandbox Code Playgroud)

如上所示,子布局内容应该包含在beginContent()和中endContent().传递给的参数beginContent()指定父布局是什么.它可以是布局文件或别名.使用上述方法,您可以在多个级别嵌套布局.

http://www.yiiframework.com/doc-2.0/guide-structure-views.html#nested-layouts