开发者

Code Igniter Views Remember Previous Variables!

I have the following code in a Controller:

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

View:

<?php
    echo $what;
?>

The Result when running this code is:

testtest

Shouldn't it be simply 'test' because the second time I am not passing the variable $data? How can I make CodeIgniter behave this way?

EDIT1:

I have come up with a temporary workaround for 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!
*
*/ 

With:

 /*
 * Flush the buffer... or buff the flusher?
 *
 * In order to permit views to be nested开发者_C百科 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]);
   }
 }

That should remove the variables from the cache after they're done being used.

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


That is interessting, I never came to use it like this but you are right it should not do this, maybe this is some caching option. In worst case you must call it like this:

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

Edit:

I have just checked the Code Igniter code from their repository. The reason for this is that they are really caching the 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 it correctly you must do it unfortunately like this:

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


codeigniter does that by default. I was searching why and then I found this

empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
extract($this->_ci_cached_vars);

on the loader class, but you can use

$this->load->clear_vars();

This will clear the buffer and solve the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜