Welcome Guest, Not a member yet? Register   Sign In
unexpected dynamic data behavior when loading views
#1

(This post was last modified: 04-14-2016, 03:55 PM by sneakyimp. Edit Reason: added code output to clarify )

It would appear that data I supply to one view sticks around when I load my second view. This is unexpected. If I supply an array of data when loading a view, I expect that data to dictate which variables are defined when the view gets processed.

For example, I have a controller method:
PHP Code:
    public function somemethod() {
        
$foo = array(
            
"val1" => "one",
            
"val2" => "two",
            
"val3" => "three"
        
);
        
$this->load->view("jtest/scope"$foo);
        
        
$bar = array(
            
"val1" => "changed one",
            
"val2" => "changed two"
        
);
        
$this->load->view("testview"$bar);
    } 
NOTE that I have loaded the same view twice but have supplied entirely different arrays of data each time. In the second case, I do not define val3 at all.
Here is testview:
PHP Code:
<?php
/**
 * view to test scope when loading views in CI
 */
?>
<div>val1=<?php echo $val1?></div>
<div>val2=<?php echo $val2?></div>
<div>val3=<?php echo $val3?></div> 

The output renders the view twice, as expected, but the second time it's rendered, val3 still has the value that was loaded the first time around. The output:
Code:
val1=one
val2=two
val3=three
val1=changed one
val2=changed two
val3=three


This val3 value is also still around if I use an entirely different view the second time:
PHP Code:
    public function scope() {
        
$foo = array(
            
"val1" => "one",
            
"val2" => "two",
            
"val3" => "three"
        
);
        
$this->load->view("jtest/scope"$foo);
        
        
$bar = array(
            
"val1" => "changed one",
            
"val2" => "changed two"
        
);
        
$this->load->view("jtest/scope2"$bar);
    } 

It would appear that somehow these vars are cached. This not at all the behavior I would expect and it does not appear to be described in documentation. It may also present a security issue to have any sensitive values cached.

Is there some feature I don't know about that would 'wipe' the data supplied to my view? Seems to me that if you supply data to a view by using an array, then the view should receive that data and nothing more.
Reply


Messages In This Thread
unexpected dynamic data behavior when loading views - by sneakyimp - 04-14-2016, 03:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB