Yii:使用与控制器的布局不同的布局渲染动作 [英] Yii: Render action using different layout than controller's layout

查看:70
本文介绍了Yii:使用与控制器的布局不同的布局渲染动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Yii中,有没有一种方法可以使用不同于为控制器定义的layout渲染单个动作?我有一个动作,我想与其他动作格式化,如果可能的话,从文档中还不清楚.

In Yii, is there a way to render a single action using a different layout than that defined for the controller? I have an action that I would like to format differently from the rest, and it's not clear from the documentation if that's possible.

推荐答案

我相信在该操作上您可以调用$layout变量.

I believe on that action you could just call the $layout variable.

public function actionYourAction()
{
    $this->layout = 'nameOfYourAltLayout';
}

下面的链接中的说明指示您将必须为每个操作设置此变量,因为您不仅可以设置默认的公共变量,还希望其他操作恢复为默认值.

The instructions in the link below indicate that you will have to set this variable for every action since you can't just set the default public variable and expect the other actions to default back to this.

http://www.yiiframework.com/wiki/28/how-to-implement-multiple-page-layouts-in-an-application/

:::

似乎最好的做法是在视图脚本中为调用它的特定操作定义$layout变量.例如,如果您的操作调用viewscriptone.php,则viewscriptone视图文件将包含:

It seems the best practice here is to define the $layout variable in the view script for the particular action that calls it. For example, if your action calls viewscriptone.php then the viewscriptone view file would contain:

$this->layout = 'nameOfYourAltLayout';

在这里覆盖比在控制器操作中覆盖更有意义.但是,正如LDG所说,如果布局是有条件的,则应将其保留在控制器中.该信息仍可以在页面使用布局"部分下面的链接中找到.

It makes more sense to override here rather than in the controller action. However, as LDG said, if the layout is conditional you should probably keep it in the controller. This information can still be found in the link above under the "Using Layouts" section of the page.

这篇关于Yii:使用与控制器的布局不同的布局渲染动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆