Welcome Guest, Not a member yet? Register   Sign In
Loading multiple views concurrently - $data array can't be used unless it's loaded in the first!
#7

[eluser]Phil Sturgeon[/eluser]
Data is stored in $this->load->_cached_vars after $this->load->view() or $this->load->vars() has been run, which means all later view files have access to it. That is what the user guide means by saying $this->load->vars() is the same as load view.

When you load a view, it includes the PHP file, runs an extract on $this->load->_cached_vars so $this->load->_cached_vars['foo'] becomes $foo, then stores the HTML. If a variable is not yet available, it will not work.

doing things this way means that if I include a view with $data['foo'] containing 'bar', then override that value in a second view call with 'baz' it will show 'bar' in the first view then 'baz' in the second. If they post-processed all the data against the view at the end then I would only ever see 'baz' in both locations.

It might seem illogical initially, but it makes a LOT of sense if you think about it that way.

Anyway, have a look at my CodeIgniter Template library for a much nicer way to handle templates. Just added in mobile support and some nice global data handling.

Code:
$this->template->foo = 'bar';

Shove that in your constructor, controller, wherever to make it globally available in your header, footer, main body, sidebar partials, whatever.


Messages In This Thread
Loading multiple views concurrently - $data array can't be used unless it's loaded in the first! - by El Forum - 10-04-2010, 06:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB