Welcome Guest, Not a member yet? Register   Sign In
Passing data around from view to view unlimited number of times without rebuilding the data array
#1

[eluser]Unknown[/eluser]
I'm loading views from within views multiple times and to avoid rebuilding the data array i create a self-reference to it.

$data=array();
$data['data']=&$data;

then set some other values

$data[' .. ']= ...

then load the view

$this->load->view('view1',$data);

Inside view1 i have
<?php $this->load->view('view2',$data); ?>

Inside view2 i have
<?php $this->load->view('view3',$data); ?>

And in view3 i use some of the variables in $data.

My question is: are there any reasons not to use this method?


Code example below:

Controller
Code:
function index()
{
$data=array();
$data['data']=&$data;  // self reference that i use
        $data['page_title']='title';
        $data['footer_text']='bye';
        $data['js_files']=array();
        $data['js_files'][]='/etc/main.js';
        //etc..
$this->load->view('page/index.php',$data);
}

View: page/index.php
Code:
$this->load->view('layout/header.php',$data);
...
<b>main body html</b>
...

View: layout/header.php
Code:
&lt;head&gt;&lt;title>
&lt;?php if(isset($page_title)) { echo $page_title; } else { echo ' Default Title '; } ?&gt;
&lt;/title&gt;
&lt;?php
if(isset($js_files) && is_array($js_files)) {
   $this->load->view('layout/js_files.php',$data);
}
?&gt;
&lt;/head&gt;&lt;body>


Thank you for your time!
#2

[eluser]InsiteFX[/eluser]
Controller:
Code:
function index()
{
    $data=array();
    $data['page_title']='title';
    $data['footer_text']='bye';
    $data['js_files']=array();
    $data['js_files'][]='/etc/main.js';
    //etc..

    $this->load->vars($data)    // makes it global to all views
    $this->load->view('page/index.php');
}

View: page/index.php
Code:
$this->load->view('layout/header.php');
...
<b>main body html</b>
...

View: layout/header.php
Code:
&lt;head&gt;
&lt;title&gt;&lt;?php if(isset($page_title)) { echo $page_title; } else { echo ' Default Title '; } ?&gt;&lt;/title&gt;
&lt;?php
if(isset($js_files) && is_array($js_files)) {
   $this->load->view('layout/js_files.php');
}
?&gt;
&lt;/head&gt;

&lt;body&gt;
#3

[eluser]wr5aw[/eluser]
I just posted a simple registry class on github. You can autoload it and use it as a global variable container.

Here's the CodeIgniter wiki page: http://codeigniter.com/wiki/Registry

After you load it, you can do this:

Code:
// store a value
$value = 'Hello World!' // mixed, can be anything
$this->registry->set('mykey', $value);

// retrieve a value
$myvar =  $this->registry->get('mykey');

// query the registry
if ($this->registry->contains('mykey'))
{
    // do something
}

// method chaining
$value1 = 'Hello World!'
$value2 = "It's a great day."
$this->registry
    ->set('mykey1', $value1)
    ->set('mykey2', $value2)
;

#4

[eluser]InsiteFX[/eluser]
CodeIgniters $this->load->vars also makes them global to all Controllers and Views.
#5

[eluser]wr5aw[/eluser]
$this->load->vars is a one shot solution. I think I wrote the registry because I needed something more flexible.




Theme © iAndrew 2016 - Forum software by © MyBB