你可能会发现我写的这个模型很有用,需要修改。
$this->asset->load('item')
如果item 在$this->assets 数组中,它将从那里加载。
否则你必须传递一个 url 或名称:$this->asset->load('item.js');
你也可以传递一个数组$this->asset->load(array('item1.js', 'item2.css'));
它将使用 CI 中的 link_tag 函数和自定义 script_tag 函数并返回一个包含适当 js/css <script> 和 <link> 标记的字符串。
将需要对常量ASSET_DIR、ROOT 进行一些修改和定义。但是功能就在那里。
class Asset_loader_model extends CI_Model {
private $types = array('css', 'js');
private $assets = array();
public function __construct() {
parent::__construct();
$this->assets = $this->n_config->get('plugin_assets');
}
/**
* Loads an asset or assets by key
*
* @param string $asset
* @param boolean $return Default to false
*/
public function load($asset, $return = false) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $src) {
$out .= $this->load($src, true);
}
} else {
if (isset($this->assets[$asset])) {
// asset is in the plugins array
$out .= $this->__load($this->assets[$asset]);
} elseif (is_file(ROOT . ASSETS_DIR . $asset)) {
// asset must be a /js/include.js
$out .= $this->__load($asset);
} else {
trigger_error("Could not find asset {$asset}.", E_USER_WARNING);
}
}
if ($return) {
return $out;
} else {
echo $out;
}
}
/**
* Loads a single asset array by key
*
* @param key $asset
* @return string
*/
private function __load($asset) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $type => $srcs) {
if (!in_array($type, $this->types)) {
continue;
}
if (is_array($srcs)) {
foreach ($srcs as $src) {
$out .= $this->load_individual($src, $type);
}
} else {
$out .= $this->load_individual($srcs, $type);
}
}
} else {
$type = $this->get_type($asset);
if (in_array($type, $this->types)) {
$out .= $this->load_individual($asset, $type);
}
}
return $out;
}
/**
* Gets type based on extension
*
* @param string $src
* @return string $type
*/
public function get_type($src) {
return pathinfo($src, PATHINFO_EXTENSION);
}
/**
* Outputs src in proper html tag
*
* @param string $src
* @param string $type
*/
private function load_individual($src, $type) {
if (!is_http($src)) {
$src = base_url(ASSETS_DIR . $src);
}
switch ($type) {
case 'js':
return script_tag($src);
break;
case 'css':
return link_tag($src);
break;
}
}
}
资产助手:
if (!function_exists('script_tag')) {
/**
* Script
*
* Generates link to a JS file
*
* @param mixed $src Script source or an array
* @param bool $index_page Should index_page be added to the js path
* @return string
*/
function script_tag($src = '', $index_page = false) {
$CI = & get_instance();
$script = '<script ';
if (is_array($src)) {
foreach ($src as $k => $v) {
if ($k === 'src' && !preg_match('#^([a-z]+:)?//#i', $v)) {
if ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($v) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $v . '"';
}
} else {
$script .= $k . '="' . $v . '" ';
}
}
} else {
if (preg_match('#^([a-z]+:)?//#i', $src)) {
$script .= 'src="' . $src . '"';
} elseif ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($src) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $src . '"';
}
}
return $script . ' type="text/javascript"' . "></script>";
//return $script . '></script>'; // w3c no need for type
}
}
/**
* Checks if a string contains a url
*
* @param string $str
* @return boolean
*/
function is_http($str) {
return preg_match("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", $str);
}