Yii interface passes variable from controller to view

To pass a variable to view mode, I use:

$this->render('login', array('model' => $model));

      

But I also need to acces this variable in the footer.php part of the template:

I am trying this:

$this->render('footer', array('model' => $model));

      

But when in footer.php I try the acces variable, I get an "undefined variable" error

What's wrong?

+3


source to share


3 answers


Templates in yii get data from a controller using a link $this

.

<?php
SomeController extends Controller {
    public $something;

    public function init() {
        $this->something = 'qwerty';
    }
    public function actionA() {
        $this->render('view', array('model' => $model));
    }
}

      

In the template:



<?php echo $this->something; ?>

      

Pay attention to the default templates yii. Breadcrimbs are rendered using a controller property, so this is the best way to achieve it.

+5


source


You can use a controller class to pass variables in your view template, for example

Controller:

SomeController extends Controller {
    
    public function actionIndex() {
          $var1 = 'abc';
          $var2 = '123';
           $this->render('view', 
                           array('var1' => $var1,
                                 'var2' => $var2,
                                ));
    }
}
      

Run codeHide result




In your view template file, you can access these two variables ($ var1 and $ var2) with its name:

echo $var1; 

echo $var2 
      

Run codeHide result


+5


source


The mvc structure does not redirect the direct page.

you create first footer action and after redirect to page.

you see a demo of login action in site_controller.php

follow this

0


source







All Articles