Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send array from controller to layout header in yii?

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?

like image 882
unknownbits Avatar asked Nov 30 '25 19:11

unknownbits


2 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
like image 200
Burhan Çetin Avatar answered Dec 02 '25 16:12

Burhan Çetin


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>
like image 28
Leonardo Delfino Avatar answered Dec 02 '25 17:12

Leonardo Delfino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!