Layout 如何将数组从控制器发送到yii中的布局标头?

Layout 如何将数组从控制器发送到yii中的布局标头?,layout,view,yii,controller,Layout,View,Yii,Controller,我可能错了。但当控制器将数组数据发送到视图文件时,是否可以在YII中将数组从控制器发送到布局->header.php。正如yii的一位员工所提到的,这是“设计的”。但是,您可以在控制器类中声明一个变量,并通过 public $variable; public function actionView(){ $model = blabla::model()->findbypk($id); $this->variable = $model->name; $t

我可能错了。但当控制器将数组数据发送到视图文件时,是否可以在YII中将数组从控制器发送到布局->header.php。正如yii的一位员工所提到的,这是“设计的”。但是,您可以在控制器类中声明一个变量,并通过

public $variable;

public function actionView(){
    $model = blabla::model()->findbypk($id);
    $this->variable = $model->name;
    $this->render('view', array('model'=>$model));
}

and then, you can use $this->variable in your layout
your html code here ...

   <h1> <?php echo $this->variableName; ?> </h1>
您的html代码在此。。。

注意:控制器类中的变量或属性是静态的,因此这种方法没有太大的灵活性。

我遇到了与您相同的问题。需要向我的布局传递一个变量,该变量定义在菜单中呈现哪些项

在我的例子中,我决定创建一个小部件

我在配置文件中导入了:

'import'=>array(
    'application.widgets.Menu'
)
菜单类:

class Menu extends CWidget{

            public function init(){}

            public function run(){

                $rs = Yii::db()->menus->find(array('profile' => Yii::app()->user->profile));

                $this->render('menu', array(
                    'menus' => $rs['menus']
                ));
            }

        }
和菜单视图:

foreach($menus as $key=>$menu): ?>

    <li>
        <a href="<?php echo $menu['url']; ?>"><?php echo $menu['name']; ?></a>
    </li>

<?php endforeach; ?>
foreach($key=>$menu的菜单):?>
  • 在布局中,我导入了小部件:

    <ul class="menus">
           $this->widget('application.widgets.Menu');
    </ul>
    
      $this->widget('application.widgets.Menu');

    我可以请您详细描述一下吗?布局是网站的整个页面,视图是其中的一部分,那么如何在视图之外的部分使用该数组,例如在侧边栏或页眉或页脚中?