Codeigniter – how to load view into another view codeigniter 2.1


Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say …ej pagination and others) i did something like this

$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);

the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything… I have no idea honestly of this problem, did anybody had the same problem before?

Best Solution

Two ways of doing this:

  1. Load it in advance (like you're doing) and pass to the other view

    // the "TRUE" argument tells it to return the content, rather than display it immediately
    $data['menu'] = $this->load->view('menu', NULL, TRUE);
    $this->load->view ('home', $data);
  2. Load a view "from within" a view:

    // put this in the controller
    // put this in /application/views/home.php
    echo 'Other home content';
Related Question