Welcome Guest, Not a member yet? Register   Sign In
Views have access to more than the data that's passed to them??
#1

[eluser]summery[/eluser]
I was happily coding a page with nested views within nested views.

In view file views/page/three-column:
Code:
<div id="wrapper-3col">

<div id="content" class="column">
&lt;?php $this->load->view($content_view, $content_data); ?&gt;
</div>
  
<div id="sidenav" class="column">
&lt;?php $this->load->view($sidenav_view, $sidenav_data); ?&gt;
</div>
  
<div id="sidebar" class="column">
&lt;?php $this->load->view($sidebar_view, $sidebar_data); ?&gt;
</div>
  
</div>

In controller file:
Code:
$this->data["data"]["content_view"]    = "voters/list";
$this->data['data']['content_data']['list'] = $this->List_model->zip_query($zip);

$this->data["data"]["sidenav_view"]    = "voters/voter_sidenav";
$this->data["data"]["sidenav_data"]    = array();

See how I've set sidenav_data to an empty array?

Now, in views/voters/voter_sidenav:

Code:
if ( ! empty($list) )

Because this view had no data passed to it, I would expect the $list variable to be empty. But it isn't! It appeared to be set to the same thing as $this->data['data']['content_data']['list'] -- ?!

So I had the bright idea of checking which variables were available in the view, and inserted get_defined_vars() into the view to check it out.

And I nearly had a heart attack!! For one thing, each nesting copied my data, so the bulk of my page data was in memory more than ten times!! After some research, I've decided I need to use $this->load->vars - instead of using the second parameter in $this->load->views - to partially avoid these unnecessary zillions of copies.

Even on very basic pages, the codeigniter structure seems to involve a LOT of repetition of static variables, though. For example, get_defined_vars() on the top level view of a very simple page reveals the following array (condensed)

Code:
Array() {
...
[_ci_CI] -> About object  // This is my simple controller
   ...
   [config] => CI_Config Object   // Config array
   [uri] => CI_URI Object
     ...
     [config] => CI_Config Object // 2nd copy of config array
   [router] => CI_Router Object
     ...
     [config] => CI_Config Object // 3nd copy of config array
     [uri] => CI_URI Object
       ...
       [config] => CI_Config Object // 4th copy of config array
   [load] => CI_Loader Object
     ...
     [config] => CI_Config Object   // 5th copy of config array
     [uri] => CI_URI Object
       ...
       [config] => CI_Config Object // 6th copy of config array
     [router] => CI_Router Object
       ...
       [config] => CI_Config Object // 7th copy of config array
       [uri] => CI_URI Object
         ...
         [config] => CI_Config Object // 8th copy of config array

And it goes on after that. I'm not an expert in get_defined_vars() and print_r are doing; but there's a *RECURSION* note and then the array continues with a second repetition of the entire CI_Loader Object with its four additional copies of the config array.

Using get_defined_vars() on my earlier, more complicated page suggested that hither-and-thither copying of data is not restricted to the config array but can occur all over the place. Which makes me nervous.


So questions:
- Is it really true that views can access other views' data even when it isn't passed to them? Why?
- Is it advisable to use load_vars instead of the second parameter of load->views when you do a lot of nesting?
- Why, oh why, are there so many copies of the $config array accessible from a simple, boring little view page?




Theme © iAndrew 2016 - Forum software by © MyBB