Welcome Guest, Not a member yet? Register   Sign In
Header manager (Javascript libraries, etc.)
#1

[eluser]Unknown[/eluser]
This is a library I wrote today to help with managing the large amount of javascript libraries used on our site, as well as their css files. So, here it is:

For the file 'Headers.php' under your application/libraries folder:
Code:
<?php

/**
* Allows you to easily add header items such as script includes, etc. to a page.
*
* @author Scott R. Pledger
*/
class Headers {

var $ci;
var $incl_pkgs  = array();
var $avail_pkgs = array();

public function __construct() {
  $this->ci =& get_instance();
  $this->ci->load->helper('url');
  $this->ci->load->library('session');
  $this->ci->config->load('headers');
  $this->avail_pkgs = $this->ci->config->item('headers');
  $this->incl_pkgs = array();

  $default_pkgs = $this->ci->config->item('headers_defaults');
  if($default_pkgs!==FALSE){
   foreach($default_pkgs as $pkg){
    $this->add($pkg);
   }
  }


}

public function add($pkg_name){
  if( array_search($pkg_name,$this->incl_pkgs) === FALSE){
   if( isset($this->avail_pkgs[$pkg_name])) {
    if(isset($this->avail_pkgs[$pkg_name]['depends'])){
     foreach($this->avail_pkgs[$pkg_name]['depends'] as $dependancy){
      $this->add($dependancy);
     }
    }
   }
   $this->incl_pkgs[] = $pkg_name;
  }
}

public function render($return_value = false){
  $render_str = "";

  foreach($this->incl_pkgs as $value){
   if(isset($this->avail_pkgs[$value])){

    $pkg = $this->avail_pkgs[$value];
    if(isset($pkg['css'])){
     foreach($pkg['css'] as $file_loc){
      $file_str = '<link rel="stylesheet" href="'.base_url().$file_loc.'" />'."\n";
      if($return_value){
       $render_str .= $file_str;
      } else {
       echo $file_str;
      }
     }
    }
    if(isset($pkg['javascript'])){
     foreach($pkg['javascript'] as $file_loc){
      $file_str = '[removed][removed]'."\n";
      if($return_value){
       $render_str .= $file_str;
      } else {
       echo $file_str;
      }
     }
    }
   } else {
    $file_str = $value."/n";
    if($return_value){
     $render_str .= $file_str;
    } else {
     echo $file_str;
    }
   }
  }
  if($return_value){
   return $render_str;
  }
}
}

Then, make a file called 'headers' in your application/config folder that looks like this:
Code:
<?php
$config['headers_defaults'] = array(
'site-base'
);

$config['headers']['site-base'] = array(
'javascript'=>array('files/main.js'),
'css'=>array('files/main.css'),
'depends'=>array('jquery-ui','ckeditor','jquery.datetimepicker')
);

$config['headers']['jquery'] = array(
'javascript'=>array('files/jslib/jquery-1.6.2.min.js')
);

$config['headers']['jquery-ui'] = array(
'javascript'=>array('files/jslib/jquery-ui-1.8.16.custom.min.js'),
'css'=>array('files/jquery-ui-1.8.16.custom/css/custom-theme/jquery-ui-1.8.16.custom.css'),
'depends'=>array('jquery')
);

$config['headers']['jquery.datetimepicker'] = array(
'javascript'=>array('files/jquery-ui-1.8.16.custom/js/jquery.datetimepicker.js'),
'css'=>array('files/jquery-ui-1.8.16.custom/css/custom-theme/jquery.datetimepicker.css'),
'depends'=>array('jquery-ui')
);

$config['headers']['ckeditor'] = array(
'javascript'=>array('files/ckeditor/ckeditor.js','files/ckeditor/adapters/jquery.js'),
'depends'=>array('jquery')
);

How it works:
Each $config['headers'] item represents an available client-side library. Each of these has three possible attributes:
'javascript', which is an array of javascript files that need to be included.
'css', which is an array of css stylesheets that need to be included.
'depends', which is an array of packages which must be included first.

If you have any questions/comments/suggestions for this, let me know!
#2

[eluser]porquero[/eluser]
You might use link_tag helper.
Well if you want you can implement my library that improve link_tag and add js_tag:

http://ellislab.com/forums/viewthread/207069/




Theme © iAndrew 2016 - Forum software by © MyBB