0

I recently began a new project using CodeIgniter. This is a huge project with several CSS, js and image files. So I need to know whether CodeIgniter has the capability to automatically read all the CSS and js files and include them on the page? It will be pretty hard for me to add all those file manually. If it is possible, then how?

2 Answers 2

2

Make a helper with a function like this:

function include_javascript()
{
    $dir = "JS_DIR";
    if($handle = opendir($dir))
    {
        while(false !== ($entry = readdir($handle)))
        {
            if($entry != "." && $entry != "..")
            {
                $extension = explode(".", $entry);

                if(end($extension) == "JS" || end($extension) == "js")
                {
                    // Include file
                    echo '<script type="text/javascript" src="'.site_url($dir."/".$entry).'"></script>';
                }
            }
        }
        closedir($handle);
    }
}

Then in your view:

<?php include_javascript(); ?>
Sign up to request clarification or add additional context in comments.

4 Comments

again do I have to mention all the files inside if(end($extension) == "JS") { // Include file } condition OR it will take care of all files in that folder?
It takes care of all the files in the JS_DIR with the file extension JS or js
So what do I have to write instead of //include files?
Updated the answer, echo '<script type="text/javascript" src="'.site_url($dir."/".$entry).'"></script>';
1

Yes you can do that in codeIgniter. What you need to do is create new folder outside of your application folder in codeIgniter (CSS folder, JS folder and Images Folder). You just keep all those files in respective folders. After that you just create inc folder in your views in that you just write a new view file in that include all your JS, CSS files. Like a Header file. Include that header file in every view file. There you can get all of your css properties in all of your pages with one include. For images you need to do it manually because you are going to use different images in different places. I hope this will be useful for you.

2 Comments

I know that but still I have to add all those files to one file inside inc folder. I mean is there a way just like Ruby on rails. In ruby on rails we dont have to care about including js, css files the RoR project takes care of these files. I need something similar in codeigniter.
that i don't know sorry I am following this way in my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.