4

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 ?

3 Answers 3

7

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 

Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

$model variable not declare anywhere
I guess you haven't understand the question or the answer. The OP already had $model and asked how to pass it down to the view. This is not full controller code. Read the text again. PS. This is an accepted answer from 2014 - nobody complained about its correctness for almost 5 years. Maybe you are the one who does something wrong?
0

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

2 Comments

login variable set in session
Noo, thisi s not solution

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.