Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 16 revisions

The Asset Helper is designed to make keeping track of those pesky JavaScript, image, CSS and flash files just that little bit easier. We all know that its a bad plan to keep your files in your system/ folder, so where else can they go?

By using Asset Helper they can be easily stored on the site in a seperate folder and will also give you a firm structure to keep your files all organised logically.

[b]Folder Structure[/b]

[quote]assets/ -- css/ -- image/ -- js/ -- modules/ ---- modulename/ ------ css/ ------ image/ ------ js/ ---- modulename2/ ------ css/ ------ image/ ------ js/ [/quote]

This folder structure should make sense to you. Basically you have your generic files (logos, general stylesheets, banners, buttons, etc) and you have page specific ones, such as sub-page backgrounds and section headers. These go in a module in the asset folder named whatever you want (makes sense to give it the same name as your controller though eh?).

[b]Useage[/b] [code]$this->load->helper('asset');

// Load a css file from the main asset css folder css_asset('filename.css');

// Load a image from the modulename. Images has a 3rd optional param for attributes image_asset('filename.jpg', 'modulename', array('alt'=>'Image name!', 'width'=>50));

// Load a javascript from the main folder, BUT we only want the url and not the entire HTML tag. js_asset_url('filename.js', 'modulename');

// Want something other than a image, css or js? // The third param here tells us what the type is, typename being the folder its kept in. other_asset_url('banner.swf', '', 'flash');[/code]

[b]Source Code[/b]

[code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

// ------------------------------------------------------------------------

/**

  • Code Igniter Asset Helpers
  • @package CodeIgniter
  • @subpackage Helpers
  • @category Helpers
  • @author Philip Sturgeon < [email protected] > */

// ------------------------------------------------------------------------

/**

  • General Asset Helper
  • Helps generate links to asset files of any sort. Asset type should be the
  • name of the folder they are stored in.
  • @access public
  • @param string the name of the file or asset
  • @param string the asset type (name of folder)
  • @param string optional, module name
  • @return string full url to asset */

function other_asset_url($asset_name, $module_name = NULL, $asset_type = NULL) { $obj =& get_instance(); $base_url = $obj->config->item('base_url');

$asset_location = $base_url.'assets/';

if(!empty($module_name)):
    $asset_location .= 'modules/'.$module_name.'/';
endif;

$asset_location .= $asset_type.'/'.$asset_name;

return $asset_location;

}

// ------------------------------------------------------------------------

/**

  • Parse HTML Attributes
  • Turns an array of attributes into a string
  • @access public
  • @param array attributes to be parsed
  • @return string string of html attributes */

function _parse_asset_html($attributes = NULL) {

if(is_array($attributes)):
    $attribute_str = '';

    foreach($attributes as $key => $value):
        $attribute_str .= ' '.$key.'="'.$value.'"';
    endforeach;

    return $attribute_str;
endif;

return '';

}

// ------------------------------------------------------------------------

/**

  • CSS Asset Helper
  • Helps generate CSS asset locations.
  • @access public
  • @param string the name of the file or asset
  • @param string optional, module name
  • @return string full url to css asset */

function css_asset_url($asset_name, $module_name = NULL) { return other_asset_url($asset_name, $module_name, 'css'); }

// ------------------------------------------------------------------------

/**

  • CSS Asset HTML Helper
  • Helps generate JavaScript asset locations.
  • @access public
  • @param string the name of the file or asset
  • @param string optional, module name
  • @param string optional, extra attributes
  • @return string HTML code for JavaScript asset */

function css_asset($asset_name, $module_name = NULL, $attributes = array()) { $attribute_str = _parse_asset_html($attributes);

return '&lt;link href="'.css_asset_url($asset_name, $module_name).'" rel="stylesheet" type="text/css"'.$attribute_str.' /&gt;';

}

// ------------------------------------------------------------------------

/**

  • Image Asset Helper
  • Helps generate CSS asset locations.
  • @access public
  • @param string the name of the file or asset
  • @param string optional, module name
  • @return string full url to image asset */

function image_asset_url($asset_name, $module_name = NULL) { return other_asset_url($asset_name, $module_name, 'image'); }

// ------------------------------------------------------------------------

/**

  • Image Asset HTML Helper
  • Helps generate image HTML.
  • @access public
  • @param string the name of the file or asset
  • @param string optional, module name
  • @param string optional, extra attributes
  • @return string HTML code for image asset */

function image_asset($asset_name, $module_name = '', $attributes = array()) { $attribute_str = _parse_asset_html($attributes);

return '<img src="'.image_asset_url($asset_name, $module_name).'"'.$attribute_str.' />';

}

// ------------------------------------------------------------------------

/**

  • JavaScript Asset URL Helper
  • Helps generate JavaScript asset locations.
  • @access public
  • @param string the name of the file or asset
  • @param string optional, module name
  • @return string full url to JavaScript asset */

function js_asset_url($asset_name, $module_name = NULL) { return other_asset_url($asset_name, $module_name, 'js'); }

%

Clone this wiki locally