I might be wrong.But is it possible to send array from controller to layout ->header.php in YII as controller send array data to view file?
3 Answers
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
1 Comment
I had the same problem as you. Needed to pass to my layout a variable defining which items would be rendered in a menu.
In my case I decided to create a Widget.
I imported in the configuration file:
'import'=>array(
'application.widgets.Menu'
)
the Menu class:
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']
));
}
}
and the menu view:
foreach($menus as $key=>$menu): ?>
<li>
<a href="<?php echo $menu['url']; ?>"><?php echo $menu['name']; ?></a>
</li>
<?php endforeach; ?>
In the layout I imported the widget:
<ul class="menus">
$this->widget('application.widgets.Menu');
</ul>
Comments
Unfortunately you cannot pass a variable from controller to layout. It is "by design" as mentioned to by one of the yii staff. You can however, declare a variable in your controller class and call that variable in your layout by
your html code here ...
<h1> <?php echo $this->variableName; ?> </h1>
Note: the variable or the property in your controller class is static so there's not much of a flexibility in this approach.