-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Asset Manager
[b] So I was checking out some of the assets extensions people have made and though they were useful and served their purpose, I had some additions I wanted to make and so I went off to make my own asset system. Here is the scoop: [/b]
[b] So I wanted to be able to use the CodeIgnitor system in my javascript and css files and I wanted a simple way to incorporate this as well as load images, js, css, swf, flv, etc... So my system is as follows. [/b]
[b] The files used are: [/b]
[b] application>controllers>asset_controller.php application>config>assets.php application>libraries>Assets.php [/b]
[h4] and a simple adjustment to our application>config>routes.php file: [/h4]
[code]$route["asset/:any"] = "loader/load_files";[/code]
[h3] asset_controller.php [/h3]
[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Assets {
var $javascript = array();
var $css = array();
function Assets($config=array()){
// Get CI instance
$this->ci =& get_instance();
if(!empty($config)){
foreach($config as $k => $v){
$this->$k = $v;
}
}else{
show_error("Assets Config File is Missing or is Empty");
}
}
function load($file,$level="",$params=array()){
if(!$level){
$level = $this->default_level;
}
$type = strtolower(substr(strrchr($file, '.'), 1));
if($type=="jpg" || $type=="png" || $type=="gif"){
// Generate Image Link
$image_link = site_url("$this->assets_folder/$level/images").$file;
// Generate the Paramaters
$image_params = $this->generate_params($params);
// Create Image Tag
$output = "<img src='$image_link'$image_params />";
return $output;
}elseif($type=="js"){
if(array_key_exists("extra",$params) && $params["extra"] != ""){
$this->javascript[] = "$type/$level/$file/{$params["extra"]}";
}else{
$this->javascript[] = "$type/$level/$file";
}
}elseif($type=="css"){
$this->css[] = "$type/$level/$file";
}else{
return false;
}
}
function url($file,$level=""){
if(!$level){
$level = $this->default_level;
}
$type = strtolower(substr(strrchr($file, '.'), 1));
if(array_key_exists($type,$this->asset_types)){
foreach($this->asset_types as $asset_type => $folder){
if($type == $asset_type){
$output = site_url("$this->assets_folder/$level/$folder").$file;
return $output;
break;
}
}
}else{
show_error("$type is not a valid asset type");
}
}
function generate_params($params){
$output = '';
if(!empty($params)){
foreach($params as $k => $v){
$output .= ' '.$k.'="'.$v.'"';
}
}
return $output;
}
function display_header_assets(){
$output = '';
foreach($this->javascript as $file){
$output .= "\n";
}
foreach($this->css as $file){
$output .= "<link type='text/css' rel='stylesheet' href='".site_url("asset").str_replace(".css","",$file)."/' />\n";
}
return $output;
}
}
?> [/code]