• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to prevent variables bleeding into subsequent views?

#1
[eluser]rf_dev[/eluser]
Is there a way to "empty" all the variables available to a view?

Ok, let me explain. I'm not entirely sure of the best way to describe this issue. This may well be covered elsewhere in this forum but I may not be using the right terms to find it.

Lets say I call a view like this:

Code:
$html = $this->load->view('foo', array('id'=>1), true);

and then I call another one:

Code:
$htmlb = $this->load->view('bar', array(), true);

Within the 'bar' view there's a conditional part which depends on whether an id is set:

Code:
<?php //foo.php
?>
The ID is: <?php echo $id ?>
// output - The ID is: 1

Code:
<?php //bar.php
if(isset($id)){
        echo 'The ID is still '.$id;
        // output - The ID is still: 1
}
?>

The problem is, '$id" is ALWAYS set even though I didn't pass one in. It appears as a hangover from the previous call to a view, any view. Is there a way to stop that happening, seems like a major flaw to me.

Cheers
Lee

#2
[eluser]Otemu[/eluser]
Check out Modular Extensions - HMVC
https://bitbucket.org/wiredesignz/codeig.../wiki/Home

#3
[eluser]rf_dev[/eluser]
Hmm,
It's something I may look into further down the line but the project is too far progressed and near deadline to start modifying the underlying framework I'm building.

What I've posted above is just a pared down example of the behaviour I've encountered. The reality it a little bit more involved. For the time being I've had to just make sure that the call to bar precedes the call to foo, but that seems like a very precarious solution. The output of two blocks of html should not be affected by the order in which I call the views.

I think this is a big honking bug in CI.
Even if there were some underlying property I could empty to ensure clean data going to my views that would still be a hack.

Thanks for your suggestion though.

#4
[eluser]TWP Marketing[/eluser]
Is it possible that you have loaded $id as a global var in your controllers? If you use a base controller, check it also.
From the User Guide:

Quote:$this->load->vars($array)

This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

#5
[eluser]LuckyFella73[/eluser]
In system/core/Loader.php you find this:

Quote:/*
* Extract and cache variables
*
* You can either set variables using the dedicated $this->load_vars()
* function or via the second parameter of this function. We'll merge
* the two types and cache them so that views that are embedded within
* other views can have access to these variables.
*/

That meens that it is suppost to be like that. When thinking about that it's
somehow "inconsistent" to use the same variable in different partials (called within one method)
while the same variable should be set in the one and not in an other (or have a different value). Would make sense to
give it a different name for it's not "the same" variable then. You know what I mean?

#6
[eluser]rf_dev[/eluser]
No, pretty sure I haven't done that, since using my stripped down example above, if you change the order of the calls to 'foo' and 'bar' views so that 'bar' comes first, there's no $id variable available to the view.

The only $id variable is that created in the array sent to the 'foo' view.

#7
[eluser]rf_dev[/eluser]
[quote author="LuckyFella73" date="1348158350"]When thinking about that it's
somehow "inconsistent" to use the same variable in different partials (called within one method)
while the same variable should be set in the one and not in an other (or have a different value). Would make sense to
give it a different name for it's not "the same" variable then. You know what I mean?[/quote]

I disagree. The scenario in which this has manifested itself for me is that one view is outputting a list of database records so each $row['id'] turns into $id.

Then in an adjacent region, the full record for an individual item is displayed, and naturally it also gets passed $row['id'].

However, in this case it's a null record so should be rendering the edit form for a new record, however the $id is still hanging around from the list of items rendered previously.

#8
[eluser]TWP Marketing[/eluser]
The first instance of the var $id is made available to all subsequent views.
If you don't want it to be available, then explicitly reset it:

Code:
$data = array(
  'id'=>'', // or 'id'=null,
);
$this->load->view('mynextview',$data);

#9
[eluser]LuckyFella73[/eluser]
I guess the views returned (without beeing rendered) can only get the
value (or cached value) from the point you send them to the load->view
method as a second parameter. The caching doesn't work "backwards".

#10
[eluser]rf_dev[/eluser]
[quote author="TWP Marketing" date="1348159115"]The first instance of the var $id is made available to all subsequent views.
If you don't want it to be available, then explicitly reset it:

Code:
$data = array(
  'id'=>'', // or 'id'=null,
);
$this->load->view('mynextview',$data);
[/quote]

On a real page load I might load a dozen different views. Several views might be called as the result of database queries. It really isn't a viable solution to go nulling every single variable that might or might not have been used in any one of those dynamically called views.

That. Is a kludge.

I really can't see how it's not a problem that CI bakes in whole new namespacing issues and expects the user to unpick them. I call bug as I can't see where this behaviour could possibly be described as expected or desirable.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.