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

Edited by ricky spires
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.