Skip to content

Instantly share code, notes, and snippets.

@raidenz
Last active May 5, 2016 06:52
Show Gist options
  • Save raidenz/0adb45ca35d2fd35824534e7e80c00a8 to your computer and use it in GitHub Desktop.
Save raidenz/0adb45ca35d2fd35824534e7e80c00a8 to your computer and use it in GitHub Desktop.
codeigniter dynamic css and js
//credit http://jamshidhashimi.com/2013/04/12/dynamically-add-javascript-and-css-files-in-codeigniter-header-page/
// add to config
$config['header_css'] = array('style.css','prettyPhoto.css','nivo-slider.css');
$config['header_js'] = array('core.js','core.js',
'jquery-1.4.1.min.js',
'jquery-slidedeck.pack.lite.js',
'jquery-prettyPhoto.js',
'jquery.nivo.slider.js');
// add to function helper
//Dynamically add Javascript files to header page
if(!function_exists('add_js')){
function add_js($file='')
{
$str = '';
$ci = &get_instance();
$header_js = $ci->config->item('header_js');
if(empty($file)){
return;
}
if(is_array($file)){
if(!is_array($file) && count($file) <= 0){
return;
}
foreach($file AS $item){
$header_js[] = $item;
}
$ci->config->set_item('header_js',$header_js);
}else{
$str = $file;
$header_js[] = $str;
$ci->config->set_item('header_js',$header_js);
}
}
}
//Dynamically add CSS files to header page
if(!function_exists('add_css')){
function add_css($file='')
{
$str = '';
$ci = &get_instance();
$header_css = $ci->config->item('header_css');
if(empty($file)){
return;
}
if(is_array($file)){
if(!is_array($file) && count($file) <= 0){
return;
}
foreach($file AS $item){
$header_css[] = $item;
}
$ci->config->set_item('header_css',$header_css);
}else{
$str = $file;
$header_css[] = $str;
$ci->config->set_item('header_css',$header_css);
}
}
}
if(!function_exists('put_headers')){
function put_headers()
{
$str = '';
$ci = &get_instance();
$header_css = $ci->config->item('header_css');
$header_js = $ci->config->item('header_js');
foreach($header_css AS $item){
$str .= '<link rel="stylesheet" href="'.base_url().'css/'.$item.'" type="text/css" />'."\n";
}
foreach($header_js AS $item){
$str .= '<script type="text/javascript" src="'.base_url().'js/'.$item.'"></script>'."\n";
}
return $str;
}
}
//set in view
add_css(array('jamshid.css','hashimi.css'));
add_js('jamshid.js');
@Amit-9841884840
Copy link

Thank's for such a great code you had share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment