Welcome Guest, Not a member yet? Register   Sign In
variables in views
#1

[eluser]Zion.b.y[/eluser]
if you call
$this->view('blocks/someblock', array('x' => 123));
the $x will be available with the value of 123

if after that you will call
$this->view('blocks/someblock');
then the variable $x will still be defined with the value of 123 because the extract function created it in the first call

the solution is this
In class CI_Loader, line 835 , between
include($_ci_path); // include() vs include_once() allows for multiple views with the same name
and
log_message('debug', 'File loaded: '.$_ci_path);

you should place this:

foreach ($_ci_data['_ci_vars'] as $key => $value){
unset($this->_ci_cached_vars[$key]);
unset($$key);
}

this will unset all the block params and they will not be available in the next call to the block

Can someone please show this to codeigniter team so they can fix it in the next version

Thanks.

P.S.
if you have this problem you can extend the class in
application/core/My_Loader.php

and this is the code you should place there:
<?php
Class My_Loader extends CI_Loader{

protected function _ci_load($_ci_data)
{
$result = parent::_ci_load($_ci_data);
foreach ($_ci_data['_ci_vars'] as $key => $value){
unset($this->_ci_cached_vars[$key]);
unset($$key);
}
return $result;
}
}

#2

[eluser]Tim Brownlaw[/eluser]
I don't quite see the case where you'd be calling the same view with and without data...

But if there is the case and you implement your patch - what you have forgotten to mention is the need to use something like this in your view

Code:
<?php echo isset($x)?$x:'';  // '' or whatever the default is meant to be?>

If you don't you'll be generating an error message asking you what $x is meant to be!
#3

[eluser]CroNiX[/eluser]
I seriously doubt that change will make it into CI as it's not broken and supposed to work that way and has since the beginning. If you are using vars that might not be present, do what Tim suggested (and what you would normally do anyway in php) by checking to see if the variable isset() before using it. Would you just use $_POST['something'] without checking to see if it existed?

You're proposed change would actually break a lot of apps that actually rely on that and you are the first that I've seen who views this as a problem. For instance, some of my apps show the username in the top left if the user is logged in and if not it doesn't. The same thing for the user menus depending on their permission levels. That means it might exist and might not, so I need to check with isset() in the views to make sure.




Theme © iAndrew 2016 - Forum software by © MyBB