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!


Messages In This Thread
Passing data around from view to view unlimited number of times without rebuilding the data array - by El Forum - 02-13-2012, 02:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB