我在codeigniter中有一个模型,我基本上希望能够将一个文件或文件数组输入到该函数中,然后为该文件呈现正确的html。我正在使用strchr
查找.
的最后一次出现,然后获取所有内容。但是这不起作用,因为我认为它会根据php文档(函数总是返回false)。
以下是模型:
<?php
class Asset_Load_Model extends CI_Model {
public static $jsWrapperStart = '<script type="text/javascript" src="';
public static $jsWrapperEnd = '"></script>';
public static $cssWrapperStart = '<link rel="stylesheet" type="text/css" src="';
public static $cssWrapperEnd = '">';
public static $imageTypes = array ( 'gif', 'jpeg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc', 'aiff', 'wbmp', 'xbm');
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
/**
* Load an asset based on extension in string
*
* @param string/array $file
* @param string $class for image types only
*/
public function loadAsset($file, $class = '') {
$out = '';
if(is_array($file)) {
$files = array();
foreach ($files as $file) {
$out .= $this->loadAsset($file);
}
return $out;
} else {
$ext = strrchr($file, '.');
if ($ext == 'css') {
return self::cssWrapperStart . base_url() . $file . self::cssWrapperEnd;
} elseif ($ext == 'js') {
return self::jsWrapperStart . base_url() . $file . self::jsWrapperEnd;
} elseif (in_array($ext, self::$imageTypes)) {
$class = isset($class) ? ' class="' . $class . '"' : '';
return '<img alt="" src="' . base_url() . $file . '"' . $class . '>';
} else {
return false;
}
}
}
}
?>
样本用法:
$dataFooter['page_level_plugins'] = $this->asset_load_model->loadAsset(array('assets/plugins/bootstrap-modal/js/bootstrap-modal.js', 'assets/plugins/bootstrap-modal/js/bootstrap-modalmanager.js', 'assets/plugins/jquery-validation/dist/jquery.validate.min.js', 'assets/plugins/select2/select2.min.js'));
答案 0 :(得分:1)
$files = array();
foreach ($files as $file) {
$out .= $this->loadAsset($file);
}
您在$files
之前将foreach
声明为空数组,因此不会向loadAssets
发送任何内容。
尝试类似
的内容foreach ($file as $f) {
$out .= $this->loadAsset($f);
}
答案 1 :(得分:0)
<?php
class Asset_Load_Model extends CI_Model {
public static $jsWrapperStart = '<script type="text/javascript" src="';
public static $jsWrapperEnd = '"></script>';
public static $cssWrapperStart = '<link rel="stylesheet" type="text/css" href="';
public static $cssWrapperEnd = '">';
public static $imageTypes = array ( 'gif', 'jpeg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc', 'aiff', 'wbmp', 'xbm');
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
/**
* Load an asset based on extension in string
*
* @param string/array $file
* @param string $class for image types only
*/
public function loadAsset($file, $class = '') {
$out = '';
if(is_array($file)) {
foreach ($file as $f) {
$out .= $this->loadAsset($f);
}
return $out;
} else {
$ext = substr(strrchr($file, '.'), 1);
if ($ext == 'css') {
return self::$cssWrapperStart . base_url() . $file . self::$cssWrapperEnd;
} elseif ($ext == 'js') {
return self::$jsWrapperStart . base_url() . $file . self::$jsWrapperEnd;
} elseif (in_array($ext, self::$imageTypes)) {
$class = isset($class) ? ' class="' . $class . '"' : '';
return '<img alt="" src="' . base_url() . $file . '"' . $class . '>';
} else {
return false;
}
}
}
}
?>