Wizard
Wizard

Reputation: 11265

Yii framework pass variable from controller to view

To pass variable to login view I use:

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

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

I try this:

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

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

What is wrong ?

Upvotes: 4

Views: 17637

Answers (3)

Manoj Singh
Manoj Singh

Reputation: 262

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

Controller :

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

In view template file you can access these 2 variables ( $var1 & $var2) with its name :

echo $var1; 

echo $var2 

Upvotes: 7

rzelek
rzelek

Reputation: 4013

Templates in yii are getting data from controller by $this reference.

<?php
SomeController extends Controller {
    public $something;

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

In template:

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

Please take a look on default templates of yii. Breadcrimbs are displayed using property from controller, so this is propably the best way to achieve it.

Upvotes: 5

Manish Jesani
Manish Jesani

Reputation: 1347

mvc structure not redirect direct page.

you create first footer action and after redirect to page.

you see demo show login action in site_controller.php

follow this

Upvotes: 0

Related Questions