title: Can't load modules from libraries in CodeIgniter HMVC
body: <p>I'm having a problem loading modules inside my <strong>template</strong> library in CodeIgniter HMVC. The reason I want to load a module in the template library is that I wish to use modules for sideboxes and other content boxes in my template.</p>
  
  <p><strong>PS:</strong> I am also using the <strong>Smarty</strong> template parsing system for CodeIgniter, but I doubt it has anything to do with the errors, but if you have reasons to believe otherwise, please, let me know.</p>
  
  <h2><strong>What I tried to do</strong></h2>
  
  <p>I tried to load the module in two different ways, and both presented with the same errors.</p>
  
  <h2><strong>The errors</strong></h2>
  
  <blockquote>
    <p>A PHP Error was encountered</p>
    
    <p>Severity: Notice</p>
    
    <p>Message: Undefined Property CI::$template</p>
    
    <p>File: <strong>MX/Loader.php</strong></p>
    
    <p>Line Number: <strong>141</strong></p>
  </blockquote>
  
  <p>-</p>
  
  <blockquote>
    <p>A PHP Error was encountered</p>
    
    <p>Severity: Notice</p>
    
    <p>Message: Undefined Property CI::$template</p>
    
    <p>Filename: <strong>MX/Controller.php</strong></p>
    
    <p>Line number: <strong>57</strong></p>
  </blockquote>
  
  <p>-</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function load_content() on a non-object in E:\Xampp\htdocs\firecms\application\modules\sidebar_login_box\controllers\sidebar_login_box.php on line 7</p>
  </blockquote>
  
  <p>The undefined "load_content()" function will be explained further down (in <strong>Sidebar Controller</strong>).</p>
  
  <h2><strong>The Error lines</strong></h2>
  
  <p><strong>MX/Loader</strong></p>
  
  <pre><code>/*Line 140*/if (isset($this-&gt;_ci_classes[$class]) AND $_alias = $this-&gt;_ci_classes[$class])
  /*Line 141*/        return CI::$APP-&gt;$_alias;
  </code></pre>
  
  <p><strong>MX/Controller</strong></p>
  
  <pre><code>/*Line 56*/public function __get($class) {
  /*Line 57*/        return CI::$APP-&gt;$class;
  </code></pre>
  
  <h2><strong>How I tried to load the modules</strong></h2>
  
  <p><strong>This was my first attempt (loading the file and instancing its class):</strong></p>
  
  <pre><code>class Template {
  //[...]
  public function load_sidebars()
  {
      $sidebars = $this-&gt;CI-&gt;cms_model-&gt;get_sidebars();
  
      foreach ($sidebars as $sidebar)
      {
          if (trim($sidebar["content"]) == "")
          {
              //An example of sidebar module name is "sidebar:login_box"
              //The function below changes the name to "sidebar_login_box" (the
              //module's folder and controller name.
              $module = str_replace(':', '_', $sidebar["module"]);
              $file_path = APPPATH.'modules/'.$module.'/controllers/'.$module.'.php';
              require_once $file_path;
              $class = ucfirst($module);
              $object = new $class();
              $module_data = $object-&gt;index();
              $this-&gt;_section_data["sidebars"][]["content"] = $module_data;
          }
          else
          {
              $this-&gt;_section_data["sidebars"][]["content"] = $sidebar["content"];
          }
      }
  }
  //[...] 
  }
  </code></pre>
  
  <p><strong>And this was my second attempt (using the loader function):</strong></p>
  
  <pre><code>public function load_sidebars()
  {
      $sidebars = $this-&gt;CI-&gt;cms_model-&gt;get_sidebars();
  
      foreach ($sidebars as $sidebar)
      {
          if (trim($sidebar["content"]) == "")
          {
              $module = str_replace(':', '_', $sidebar["module"]);
              $this-&gt;CI-&gt;load-&gt;module($module);
              $module_data = $this-&gt;CI-&gt;$module-&gt;index();
              $this-&gt;_section_data["sidebars"][]["content"] = $module_data;
          }
          else
          {
              $this-&gt;_section_data["sidebars"][]["content"] = $sidebar["content"];
          }
      }
  }
  </code></pre>
  
  <h2><strong>The sidebar controller</strong></h2>
  
  <p>This is how the sidebar controller looks like:</p>
  
  <pre><code>class Sidebar_login_box extends Fire_Controller {
  public function index()
  {
      $view_data = array();
      //The load_content function in the template library is used to parse template files
      //and return them as a string.
      return $this-&gt;template-&gt;load_content("login_box", $view_data);
  }
  </code></pre>
  
  <p>}</p>
  
  <h2><strong>The Fire Controller</strong></h2>
  
  <p>The Fire_Controller is my core controller. My core classes' prefix is Fire_ instead of MY_.</p>
  
  <p>This is how the fire controller looks like:</p>
  
  <pre><code>class Fire_Controller extends MX_Controller {
  public function __construct()
  {
      parent::__construct();
  
      //Load configurations from the database.
      $this-&gt;config-&gt;load_db_configs();
  
      //Set the timezone.
      date_default_timezone_set(config_item("timezone"));
  
      //Loads the form validation library.
      $this-&gt;load-&gt;library("form_validation");
      //Reset the Form Validation CI Object (to fix problems with HMVC CI).
      $this-&gt;form_validation-&gt;CI =&amp; $this;
  
      //To reduce load time, the template library will not be loaded in ajax
      //requests.
      if ( ! $this-&gt;input-&gt;is_ajax_request())
      {
          $this-&gt;load-&gt;library("template");
      }
  
      //Force access via SSL connection (HTTPS) if necessary.        
      if ((int)config_item('force_https') === 1)
      {
          force_https();
      }
  }
  </code></pre>
  
  <p><strong>Note:</strong> This is a very recent project of mine, which means that the framework and all third party extensions are in the most recent stable version as of January 06, 2015.</p>
  
  <p>Thank you for your time,</p>
  
  <p>Best regards.</p>