CodeIgniter Forums

Full Version: Losing $data inside views that have been included by another view, think it's HMVC related.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]boltsabre[/eluser]
Hi everyone, got a problem, I've lost my $data array inside views that have been included by other views.


Code:
//This is a view, let's call it main:
<?php echo $test;  // works fine, echos out it's value
            $this->load->view("includes/search.php"); ?>

Code:
// And the view loaded by the above view, called search.php
<?php echo $test; ?> // Fail, get a fatal error (see next line)
//A PHP Error was encountered Severity: Notice Message: Undefined variable:  test

This has me completely stumped, but think it's got something to do with HMVC and upgrading from CI version 2.0.2 to 2.1.3...

The strange thing is that variables loaded by $this->load->vars($data); inside my MY_Controller are working in "sub" views, but $data variables defined in a normal controller are not. Here is MY_Controller code just in case it adds any insights...
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

require APPPATH."third_party/MX/Controller.php";

class MY_Controller extends MX_Controller{
    
var $data; // without this MX_Controller was breaking,
//it wasn't recogniting $this->data variables at all

    function __construct(){
        parent::__construct();
        $this->is_logged_in();
    }

    //Check if this user is logged in.
    function is_logged_in(){
  $is_logged_in = $this->session->userdata('logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true){
            $data['is_logged_in'] = FALSE;
        }else{
            $data['is_logged_in'] = TRUE;
        }
        //Set a global var so this can be used in views.
  $CI =& get_instance(); $CI->load->vars($data);

        //Set a return so this can be used as a function calls in controllers.
        return $data['is_logged_in'];
    }
}

Anyone got any ideas what could be causing this strange behavior...???

El Forum

[eluser]boltsabre[/eluser]
Hmmmm, not looking good...

I've checked it out, looks like the load function in MX_Loader (line 253)
Code:
/** Load a module view **/
public function view($view, $vars = array(), $return = FALSE) {
   list($path, $view) = Modules::find($view, $this->_module, 'views/');
   $this->_ci_view_path = $path;
   return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
Is not receiving the second variable $vars...

So I can get around this issue by passing it in, in my view such as this:
Code:
<div class="twoColDivTwo">
   &lt;?php $this->load->view('includes/likeRHSColumn', $data = array('no_checklist_display' => $no_checklist_display)); ?&gt;
</div>
And hey presto, by variable $no_checklist_display has now been passed from the main view to the "sub" view "likeRHSColumn" (because the variables are available in the view called directly from the controller, but not in views called by views).

BUT BUT BUT this is NOT how it's meant to work.

It's been a hard day of debugging this crap, need to hit the sack now, but if ANYONE has an idea or can point me in the right direction I'd be extremely grateful. Thanks.