Asset Helper for CodeIgniter


/ Published in: PHP
Save to your folder(s)

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//public/.

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


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. #
  4. # Image Helper
  5. #
  6.  
  7. if ( ! function_exists('image'))
  8. {
  9. function image($src = '', $index_page = FALSE)
  10. {
  11. if ( ! is_array($src) )
  12. {
  13. $src = array('src' => $src);
  14. }
  15.  
  16. if ( ! $src['alt'] )
  17. {
  18. $src['alt'] = '';
  19. }
  20.  
  21. $img = '<img';
  22.  
  23. foreach ($src as $k=>$v)
  24. {
  25.  
  26. if ($k == 'src' AND strpos($v, '://') === FALSE)
  27. {
  28. $img .= ' src="'.ASSETPATH.'/images/'.$v.'" ';
  29. }
  30. else
  31. {
  32. $img .= " $k=\"$v\" ";
  33. }
  34. }
  35.  
  36. $img .= '/>';
  37.  
  38. return $img;
  39. }
  40. }
  41.  
  42. #
  43. # CSS Helper
  44. #
  45.  
  46. if ( ! function_exists('attach_stylesheet'))
  47. {
  48. function attach_stylesheet($file, $media = "screen")
  49. {
  50.  
  51. $src = ASSETPATH . '/stylesheets/';
  52. $src .= ( end( explode(".", $file) ) == 'css' ) ? $file : $file.'.css';
  53.  
  54. $css = '<link rel="stylesheet ';
  55. $css .= 'href="'.$src.'" ';
  56. $css .= 'type="text/css" ';
  57. $css .= 'media="'.$media.'" ';
  58. $css .= '/>';
  59.  
  60. return $css;
  61. }
  62. }
  63.  
  64.  
  65. #
  66. # Javascript Helper
  67. #
  68.  
  69. if ( ! function_exists('attach_javascript'))
  70. {
  71. function attach_javascript($file)
  72. {
  73.  
  74. $src = ASSETPATH . '/javascripts/';
  75. $src .= ( end( explode(".", $file) ) == 'js' ) ? $file : $file.'.js';
  76.  
  77. $script = '<script type="text/javascript" ';
  78. $script .= 'src="'.$src.'"></script>';
  79.  
  80. return $script;
  81. }
  82. }

URL: http://brettbergeron.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.