3

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 3

2
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
Sign up to request clarification or add additional context in comments.

1 Comment

May i ask to describe it more? The layout is whole page of the website and a view is a part of it, So how to use that array in out of that view part, for Example in Sidebar or in the header or footer?
2

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

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.