• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assets helper for load css and js file at single view

#1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('assets'))
{
//if $str is a string include url or path
//id $str is a array include all url and path
function js($str){
$js = '';
if (is_array($str)) {
foreach ($str as $s) {
if(filter_var($s, FILTER_VALIDATE_URL)){
$js .= '<script src="'.$s.'" type="text/javascript"></script>';
}else{
$js .= '<script src="'.site_url('assets/js/'.$s).'" type="text/javascript"></script>';
}

}
}else{
if(filter_var($str, FILTER_VALIDATE_URL)){
$js .= '<script src="'.$str.'" type="text/javascript"></script>';
}else{
$js .= '<script src="'.site_url('assets/js/'.$str).'" type="text/javascript"></script>';
}

}
return $js;
}
function css($str){
$css = '';
if (is_array($str)) {
foreach ($str as $s) {
if(filter_var($s, FILTER_VALIDATE_URL)){
$css .= '<link rel="stylesheet" href="'.$s.'">';
}else{
$css .= '<link rel="stylesheet" href="'.site_url('assets/css/'.$s).'">';
}
}
}else{
if(filter_var($str, FILTER_VALIDATE_URL)){
$css .= '<link rel="stylesheet" href="'.$str.'">';
}else{
$css .= '<link rel="stylesheet" href="'.site_url('assets/css/'.$str).'">';
}

}
return $css;
}
}







see more...
Reply

#2
You've placed your functions inside a conditional which checks for the existence of a function which you do not define (assets). Meanwhile, you have not checked whether the two functions you define already exist.

I would also recommend adding a second, optional parameter to css() to pass the media attribute. Additionally, it would probably be a good idea to use the self-closing form of the link element, in case someone wants to use the function for a page/site with an XHTML doctype.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.