Return to Snippet

Revision: 17171
at August 26, 2009 01:04 by brettbergeron


Initial Code
<?php

	#
	#	Image Helper
	#				

		if ( ! function_exists('image'))
		{
			function image($src = '', $index_page = FALSE)
			{
				if ( ! is_array($src) )
				{
					$src = array('src' => $src);
				}
				
				if ( ! $src['alt'] )
				{
					$src['alt'] = '';
				}

				$img = '<img';

				foreach ($src as $k=>$v)
				{

					if ($k == 'src' AND strpos($v, '://') === FALSE)
					{
						$img .= ' src="'.ASSETPATH.'/images/'.$v.'" ';
					}
					else
					{
						$img .= " $k=\"$v\" ";
					}
				}

				$img .= '/>';

				return $img;
			}
		}		
	
	#
	#	CSS Helper
	#	
	
		if ( ! function_exists('attach_stylesheet'))
		{
			function attach_stylesheet($file, $media = "screen")
			{
			
				$src  = ASSETPATH . '/stylesheets/';
				$src .= ( end( explode(".", $file) ) == 'css' ) ? $file : $file.'.css';				
				
				$css  = '<link rel="stylesheet ';
				$css .= 'href="'.$src.'" ';
				$css .= 'type="text/css" ';
				$css .= 'media="'.$media.'" ';
				$css .= '/>';
								
				return $css;
			}
		}


		#
		#	Javascript Helper
		#

			if ( ! function_exists('attach_javascript'))
			{
				function attach_javascript($file)
				{

					$src  = ASSETPATH . '/javascripts/';
					$src .= ( end( explode(".", $file) ) == 'js' ) ? $file : $file.'.js';				
										
					$script  = '<script type="text/javascript" ';
					$script .= 'src="'.$src.'"></script>';

					return $script;
				}
			}

Initial URL
http://brettbergeron.com

Initial Description
The below code helps you move your site assets (images, css, javascript) to a public folder inside the application directory of CodeIgniter.

One thing you'll need to do is define ASSETPATH to be the path to the location of your public items. For myself, I set this to system/application/<subdomain>/public/.

See some of my other snippets to get some background on the subdomain part of that path.

Initial Title
Asset Helper for CodeIgniter

Initial Tags
php, codeigniter

Initial Language
PHP