php 在 Yii2 中的视图中更改布局文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29982759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:41:23  来源:igfitidea点击:

Change layout file within a view in Yii2

phpmodel-view-controlleryii2

提问by Moid Mohd

I am doing a small project using Yii2.

我正在使用 Yii2 做一个小项目。

Suppose I have same layout (header, footer) in a view (eg site) except a login.phpin this view. I want a different or no header / footer in this file. What can I do the remove the header / footer only from this view file.

假设我在一个视图(例如site)中有相同的布局(页眉、页脚),除了login.php这个视图中的 a 。我想要这个文件中有一个不同的或没有页眉/页脚。我该怎么做才能仅从此视图文件中删除页眉/页脚。

All I could get to change layout in different views. Is it possible to change layout in a single file of a view?

我所能做的就是在不同的视图中更改布局。是否可以在视图的单个文件中更改布局?

回答by scaisEdge

Inside the relative action:

在相对动作中:

public function actionYourAction($id)
{

    $this->layout = 'yourNewLayout';

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

回答by Wade

I am a little late to the party, but you CANchange your layout from within your view. You do not have to declare it in your controller. I personally think it is better to do it in the view, because you can easily see later what is going on. If your making HTML edits, you would go into the view file, and easily be able to see which layout it is using. Putting this in the Controller, you (or someone later on) might miss the layout change nested into your controller's action.

我有点迟到了,但你CAN从您的视图中改变你的布局。您不必在控制器中声明它。我个人认为最好在视图中进行,因为稍后您可以轻松看到正在发生的事情。如果您进行 HTML 编辑,您将进入视图文件,并且很容易就能看到它正在使用哪种布局。将它放在控制器中,您(或稍后的某个人)可能会错过嵌套在控制器操作中的布局更改。

Since $thisrefers to your view in Yii2 and not your controller as it did in Yii1, the old $this->layoutdoesn't work anymore from within your view.

由于$this在 Yii2 中引用了您的视图,而不是像 Yii1 中那样引用您的控制器,因此旧$this->layout视图在您的视图中不再起作用。

Now, in Yii2, you refer to the controller from your view using $this->context.

现在,在 Yii2 中,您可以使用$this->context.

$this->context->layout = 'your-layout';

回答by xav

In my project I wanted 2 layouts: one for site and one for the webapp. As the main.php file is the default layout, I've created a site.php layout and in the beginning of the siteController, just after the class declaration, I've put

在我的项目中,我想要 2 种布局:一种用于站点,一种用于 webapp。由于 main.php 文件是默认布局,我创建了一个 site.php 布局,并在 siteController 的开头,就在类声明之后,我已经把

public $layout = 'site';

The result is that only the siteController rendered views are using the site.php layout. It worked for me.

结果是只有 siteController 呈现的视图使用 site.php 布局。它对我有用。

回答by Perry J

I'm also a litte late to the party, but struggled with this stuff today... To me, to create a separate layout just because I want to skip the footer or header seems like much code for little win. If I can stick to the main layout, I can just get at the controller and the action currently loaded, and have it omitted this way (write this in main.php):

我参加聚会也有点晚了,但今天在这些东西上挣扎......对我来说,仅仅因为我想跳过页脚或页眉而创建一个单独的布局似乎是一个小小的胜利的代码。如果我可以坚持主布局,我可以只获取控制器和当前加载的动作,并以这种方式省略它(在 main.php 中写入):

$contr   = Yii::$app->controller->id;
$action  = Yii::$app->controller->action->id;
$skipFooter = $contr == 'site' && $action == 'login'; //...or enter here   what U want

... and then later:

……然后:

<?php if (!$skipFooter): ?> //Never at login...
    <footer class="footer">
        <div class="container">
            <p class="pull-left">&copy; YourSite.com <?= date('Y') ?></p>

            <p class="pull-right"><?= Yii::powered() ?></p>
        </div>
    </footer>
<?php endif; ?>