Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does codeigniter load view files and pass variables through?

I've read through the source code but it seems a little cryptic. I'm just trying to get my head around how CI transforms an array into individual variables available to the view.

I gather that the view is included with include(), but the variables seem to only be effective for the view.

Controller:

$this->load->view('about', array('title' => 'about'));

View:

<?php echo $title; // shows 'about' ?>
like image 480
spamoom Avatar asked Mar 23 '11 13:03

spamoom


People also ask

How can you load a view in CodeIgniter?

Loading a View To load a particular view file you will use the following method: $this->load->view('name'); Where name is the name of your view file.

How pass multiple variables from controller view in CodeIgniter?

$data=array($var1,$var2); $this->load->view('myview',$data);


1 Answers

php extract() function

$array = array('test' => 'val', 'key' => 'value');

extract($array);

var_dump($test);
var_dump($key);

The variables $test and $key would be "visible" in the view only if they are declared localy, so let's say a function includes the view file and right before including it, it will extract the values, then the variables would be visible only inside that function ( witch body would contain the view file too ), it's not realy how CI does it but it explains the principle .

like image 182
Poelinca Dorin Avatar answered Oct 31 '22 19:10

Poelinca Dorin