Jump to content

codeigniter - layout manager - function not loading array


ricky spires

Recommended Posts

Hello.

 

I was doing a tutorial on how to build a layout manager for codeigniter and i have run in to a little trouble.

 

In the welcome.php controller i have added

$this->layouts->add_include('js/simple_js.js');

then in my default layout view i have added some code in the header to call the js file

<head>
  <?php echo $this->layouts->print_includes(); ?>
</head>

then in the model i have this

// Include js or css for specific pages
  private $includes = array();


// get the included js from the welcome controller
public function add_include($path, $prepend_base_url = TRUE)
  {
    if ($prepend_base_url)
    {
      $this->CI->load->helper('url'); // Load this just to be sure
      $this->file_includes[] = base_url() . $path;
    }
    else
    {
      $this->file_includes[] = $path;
    }
    
    return $this; // This allows chain-methods
  }
  



// load the array
  public function print_includes()
  {
    // Initialize a string that will hold all includes
    $final_includes = '';
    
    foreach ($this->includes as $include)
    {
      // Check if it's a JS or a CSS file
      if (preg_match('/js$/', $include))
      {
        // It's a JS file
        $final_includes .= '<script type="text/javascript" src="' . $include . '"></script>';
      }
      elseif (preg_match('/css$/', $include))
      {
        // It's a CSS file
        $final_includes .= '<link href="' . $include . '" rel="stylesheet" type="text/css" />';
      }
    }

    return $final_includes;
  }

if i add an echo inside the add_include() function i get the correct url to the file - http://my-site.com/js/simple_js.js

 

if i do the below inside the print_includes() function i get an empty array.

 echo '<pre>'; print_r($this->includes); echo '<pre/>';
Array()

 

 

so ... im guessing this is not getting the info from the add_include() function ???

 

why is the array empty ???

private $includes = array();

thanks

Ricky

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.