title: codeigniter pass data to nested view
body: <p>I have done very much research on this topic but I cannot find how i can do this. I am trying to add data to the <code>$data</code> parameter in a view that is being called from the controller of another view.  However, any data i add to the <code>subview</code> via the <code>subcontroller</code> cannot be accessed by the <code>subview</code>.  However, when I try to pass data to the subview via the client view, it works just fine.  Most of the fixes on SO seem to reference just calling the <code>$key in $data['key']</code> rather than $data so that doesn't seem really relevant here...</p>
  
  <p>I have two classes: </p>
  
  <ul>
  <li>welcome.php - a page  </li>
  <li>welcomemenu.php - a set of controls intended to
  be loaded into welcome.php</li>
  </ul>
  
  <p>Here is my Client Controller (the page that it's on, <code>welcome.php</code>), which stores the return val from subview <code>$welcomemenu</code> in its own $data array...: </p>
  
  <pre><code>&lt;?php
  
  class Welcome extends CI_Controller {
  
      function __construct() {
          parent::__construct();      
      }
  
      function index() {
          //echo 'this is the Welcome index function';
          $data['clienttestdata'] = 'data from welcome.php!';
          $data['welcomemenu'] = $this-&gt;load-&gt;view('welcome/welcomemenu', $data, true);
  
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;view('templates/header');
          $this-&gt;load-&gt;view('pages/welcome', $data);
          $this-&gt;load-&gt;view('templates/footer');
  
      }
  }
  </code></pre>
  
  <p>And here is the client view ("welcome_view.php" - seems simple enough.  The $welcomemenu var is where I put the returns from my component class...):</p>
  
  <pre><code>    &lt;section id="allWelcomeContent" class="mainBody"&gt;
          &lt;header id="mainPageHdr" class=mainPageHdr&gt;
              &lt;!-- other stuff from my header --&gt;
          &lt;/header&gt;
          &lt;!-- this is where i want to put the welcome menu... --&gt;
          &lt;section id="mainWelcomeContent" class="mainContent"&gt;
              &lt;div&gt;
                  &lt;?php echo $welcomemenu;?&gt;
              &lt;/div&gt;
          &lt;/section&gt;
      &lt;/section&gt;
  </code></pre>
  
  <p>And here is the Controller for my sub-component <code>welcomemenu.php</code>: </p>
  
  <pre><code>&lt;?php
  
  class Welcomemenu extends CI_Controller {
  
      public function __construct() {
          parent::__construct();
      }
  
      public function index() {
          $data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog'); 
          $data['testdata'] = 'data from welcomemenu.php!';
  
          $this-&gt;load-&gt;view('welcome/welcomemenu', $data);
      }
  }
  </code></pre>
  
  <p>And lastly:  Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (i.e., the $clienttestdata shows up fine but $testdata doesn't)!</p>
  
  <pre><code>&lt;section&gt;
  &lt;!-- TODO:  make this element repeatable so content can load from controller and/or model. --&gt;
  &lt;div id="divMenuItems"&gt;
      &lt;?php echo $clienttestdata;?&gt;
      &lt;?php echo $testdata;?&gt;
  &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>Still i couldn't find any proper solution. if anyone then please give me</p>