views:

33

answers:

1

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 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

A: 

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' => ''));
Vinzenz
It doesn't work with that either unfortunately! :(
Jonas
And what is if you use NULL as parameter?
Vinzenz
NULL as a parameter didn't work either. Still the same output.
Jonas
Which CI version do you use?
Vinzenz
I'm on 1.7.2 no hacks or anything. Fresh install.
Jonas
Ok I updated my post according to my findings
Vinzenz
Updated my post with a patch I wrote.
Jonas
Well I guess you should create a bug report with this patch as suggestion. http://bitbucket.org/ellislab/codeigniter/issues/new However they might have valid reasons for doing it this way. (Maybe speed and it's not a common use case I would say)
Vinzenz
I'm just having trouble believing that I'm doing everything right here. I refuse to believe that EllisLab overlooked such a major issue. I still feel like I must be missing something.
Jonas
Well as I said, it's not a common use case to call the same view twice. You could try to call 2 different views which have the same content (including the echo $what) for testing only of course. And if it behaves different, it might really be something they have overseen OR it even is by design and just done that way to increase the speed of the app.
Vinzenz
That's true. I unfortunately needed this view to be called in a loop, so this problem was unavoidable. I'll submit a bug report. Thanks
Jonas
It would be cool if you could link the issue here in your post, so that other people who might stumble upon this thread can find the result in the bug you have reported :-)
Vinzenz
@Jonas - I came here to say the same thing as Vinzenz. This is a caching issue. Just before the view loader starts the output buffer, it [extracts](http://php.net/manual/en/function.extract.php) `$this->_ci_cached_vars`. This means that all variables therein are put into the local scope, which allows you to refer to $test. I wouldn't really call this a bug, since CI is really saving a lot of memory by storing values in cache. The way to get around this is to be sure to pass the right values to your views every time, as suggested by Vinzenz.
treeface
I see, thank you for the clarification.
Jonas
@Jonas - On the other hand, if you want to brute force it to make it work the way you expect, you could always change the core to flush that cache whenever a view is loaded. This would involve simply setting `$this->_ci_cached_vars` equal to an empty array at the end of the `_ci_load()` method. It goes without saying that I don't advise this, but it would solve your problem.
treeface