Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 17 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 < phil.sturgeon@styledna.net > */

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

/**

  • 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
Clone this wiki locally