CodeIgniter的资产助手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeIgniter的资产助手相关的知识,希望对你有一定的参考价值。

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.
  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. }

以上是关于CodeIgniter的资产助手的主要内容,如果未能解决你的问题,请参考以下文章

PHP CodeIgniter的资产助手

CodeIgniter资产目录字符串助手

无法使用 codeigniter 验证码助手创建验证码

PHP Codeigniter 数据库助手类“未使用表”

我如何从 Codeigniter 中的助手调用助手?

导致资产预编译在heroku部署上失败的代码片段