Welcome Guest, Not a member yet? Register   Sign In
view variables interferring with each other
#1

[eluser]coffeeandcode[/eluser]
Hi all, my CI application loads several views as variables before passing everything off to a template view. In some of my smaller views there are variables, such as $title, which are also found in other views. It seems like using the same variable name in more than one view causes them to interfere, even though they are not passed to the view in the data array.

For example, I have a small view which is a list with a title, and one of the variables passed to it is $title. When I do this, the $title for the page (ie, main template) is changed to the list's $title, even though no title variable is present in the main template's data array.

Am I doing something wrong, or is this the way CI is supposed to work? I would think the data passed to a view would be in scope only within that view.
#2

[eluser]pickupman[/eluser]
The loader class caches all variable passed to it. So each call to this->load->view(), the array keys are cached with their values.

You may find it to do a little namespace. So if you have a form title use like
Code:
$data['form']['title'] = 'Some form title';
$data['page']['title'] = 'My Page Title';

$this->load->view('header', $data);
$this->load->view('form', $data);
$this->load->view('footer', $data);

There is actually an advantage to have the variables cached, as they can be used in any view that will be loaded without having to set it explicity.
#3

[eluser]coffeeandcode[/eluser]
Thanks, that makes sense. Is it possible to turn the loader caching off? In my case the caching makes things less convenient..
#4

[eluser]pickupman[/eluser]
It's not that it can be turned on or off. You can extend the Loader class and overwrite the section of code where it saves the key/value pairs.




Theme © iAndrew 2016 - Forum software by © MyBB