Welcome Guest, Not a member yet? Register   Sign In
Merging global and controller data array for view
#1

[eluser]Mr. Pickle[/eluser]
Hi there, I've got a question on how to merge global and controller data to one array.

I now have a MY_Controller performing tasks for every request (such as session status, getting the main navigation items from the db etc.) This writes the data necassary for all requests to
Code:
$this->data

Of course in my controller functions I also set the data specfic for the request.
There I set the data to:
Code:
$data = array(/*data array here*/);

Before sending the data to the view I want to have both arrays in one array (as you can only send one data array with your view) I do it like this:
Code:
$data = array_merge($data, $this->data)

Now I have in my views available both the global data and the controller function data.
Is this the way to do it in CI, or do I oversee some internal function/method to get this sorted better?
#2

[eluser]n0xie[/eluser]
Code:
/* MY_Controller */
    protected function render($childdata = NULL)
    {
        if(! empty($childdata))
        {
            $data = array_merge($this->data,$childdata);
        }
        else
        {
            $data = $this->data;
        }

        $this->load->view($this->template, $data );
    }

/* Example extends MY_Controller */
    function index()
    {
        $data['title'] = 'my super site';
        $this->render($data);
    }




Theme © iAndrew 2016 - Forum software by © MyBB