Jordan Elphick
Jordan Elphick

Reputation: 53

Load unique javascript files specific to code igniter view

I've been trying to find the best way to load a list of javascript files in my code igniter (CI) project.

The problem I have is that I have a large amount of CI views, with each view having a distinct list of javascript resources that need to be loaded for the view to function correctly.

How can I load the required resources for a specific view easily?

The two methods I am contemplating using are:

  1. Load every resource every time - I am hesitant to use this method as it will unnecessarily increase load times for resources that are not required.

  2. Create a (massive) switch statement that defines the list of files required for each page. However, I am hesitant to using this method as it is difficult to maintain

Are there any native functions to CI, or 3rd party tools that I can use to better solve my problem?

Kind Regards Jordan

Upvotes: 2

Views: 106

Answers (2)

Faisal Mehmood Awan
Faisal Mehmood Awan

Reputation: 436

once i was in a project with alot of js an css files upto 320 . the trick i have played is that i separate them into different folders and give each dir a unique name then i write a function which accept the dir name and in that function i have set up this code

function dirname($dir){
$data=[];
if(is_dir($dir)){
  $objects = scandir($dir);
  foreach($objects as $object){
    if($object != "." && $object != ".."){
        $data['dirname'][] = $object;         
    }
  }  
  return $data;
}

it will scan the file names and store them in array after then you can easily output them . hope this will help you

Upvotes: 2

Nilan
Nilan

Reputation: 94

I'd prefer having common CSS and JS files in the master template and then may be add the view specific assets in the view it self. Or else its better to write a condition to handle the css and js files on header. I don't think there's a tool that knows which file has to be loaded to which view without us telling. So you will have to write some logic anyway.

Upvotes: 0

Related Questions