Code Igniter Views Remember Previous Variables!

I have the following code in the controller:

$data['what'] = 'test'; $this->load->view('test_view', $data); $this->load->view('test_view'); 

View:

 <?php echo $what; ?> 

Result when running this code:

 testtest 

Isn't that just a β€œtest” because the second time I don't pass the variable $ data? How can I make CodeIgniter behave this way?

EDIT1:

I found a temporary solution to this problem:

Replace in Loader.php:

 /* * Flush the buffer... or buff the flusher? * * In order to permit views to be nested within * other views, we need to flush the content back out whenever * we are beyond the first level of output buffering so that * it can be seen and included properly by the first included * template and any subsequent ones. Oy! * */ 

Via:

  /* * Flush the buffer... or buff the flusher? * * In order to permit views to be nested within * other views, we need to flush the content back out whenever * we are beyond the first level of output buffering so that * it can be seen and included properly by the first included * template and any subsequent ones. Oy! * */ if (is_array($_ci_vars)){ foreach ($_ci_vars as $key12 => $value12) { unset($this->_ci_cached_vars[$key12]); } } 

This should remove the variables from the cache after using them.

BUG REPORT: http://bitbucket.org/ellislab/codeigniter/issue/189/code-igniter-views-remember-previous

+4
source share
1 answer

This is interesting, I never used it like that, but you're right, it should not do this, maybe this is a caching option. In the worst case, you should call it like this:

 $this->load->view('test_view', ''); 

Edit:

I just checked the Ignit Code from my repository. The reason for this is that they really cache variables:

  /* * 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. */ if (is_array($_ci_vars)) { $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); } extract($this->_ci_cached_vars) 

If I understood correctly, you should unfortunately do this as follows:

 $this->load->view('test_view', array('what' => '')); 
+1
source

All Articles