Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass variables from controller to view in Yii

Tags:

yii

I cannot use the variables specified in controller in the corresponding view. Here is my code:

public function actionHelloWorld()
    {

        $this->render('helloWorld',array('var'=>'this is me'));
    }

In the helloWorld.php (view file):

<h1>Hello, World!</h1>
<h3><?php echo $var; ?></h3>

It only prints out "Hello, World!", looks like $var is unaccessible in the view. Anyone?

like image 883
Michael Avatar asked Mar 04 '12 01:03

Michael


2 Answers

"var" is a reserved word in PHP, so you won't be able to use that name for your variable. See: http://www.php.net/manual/en/reserved.keywords.php

Try using a different variable name and it should work.

like image 146
ryanlahue Avatar answered Oct 14 '22 02:10

ryanlahue


that should work, though with any variable name other than 'var'

please note that 'this' in a view refers to its controller, so if you have a public member variable or method in a controller, you can access it from the view:

MyController.php:

class MyController extends CController{
  public $foo = 'bar';

  public function actionIndex(){
    $this->render('index');
  }
}

index.php:

<?php 

echo $this->foo; //result is bar

?>
like image 25
Neil McGuigan Avatar answered Oct 14 '22 02:10

Neil McGuigan