开发者

Including all /javascript/*.js from a directory

Can anyone tell me the best practice for including a set of files in a directory?

For instance, if I had a directory that was /javascript, and in there it had 10 javascript files, how would I go about loading the entire directory?

This question is moot now, but for future reference;

I use the following to read files in the same manner:

<?php
    session_start();

    // Auto import all js files in the directory.

            $files = glob("*.js");
            sort($files);                   

            $jsFiles = array();
            for($i = 0; $i < count($files开发者_运维百科); $i++){
                error_log('Auto-Opening: '.$files[$i]);
                $tempString = file_get_contents($files[$i]);
                //error_log('Auto-Opened Content: '.$tempString);
                array_push($jsFiles, $tempString);
            }
?>

Sorry, I guess this needs clarification:

The code I have included is used in an entirely seperate part of my project, it includes the RAW string format of a directory of javascript files for my processing purposes. You do not have to worry about why I am doing this.

I could use this to add script tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).

Please also note, as suggested:

$files = glob("{directory/*.js}",GLOB_BRACE);
                for($i = 0; $i < count($files); $i++){
                    include($files[$i]);
                }

Does nothing but echo those files to the screen, without script tags.


I would simply call include() on the files rather than doing a file_get_contents(). This is for two main reasons/benefits:

  • You are not loading the files into memory (in PHP anyway) before outputing them. This saves server resources (albeit a fairly small amount).
  • You can put PHP code into your JS/CSS etc and have it be evaluated. This means you can predefine JS variables at the server side, apply colour themes to your CSS and a multitude of other things very simply through PHP.


PHP will not output <script> tags for you. It has no idea what JS is, other than a chunk of text. Your glob version is close, but would have to be:

$files = glob("{directory/*.js}",GLOB_BRACE);
for($i = 0; $i < count($files); $i++){
    echo '<script type="text/javascript">';
    include($files[$i]);
    echo '</script>';
}


I could use this to add script tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).

If you want the output of the PHP script to get executed as javascript, you need to add a proper header:

header("Content-type: text/javascript");
foreach (glob('/path/to/files/*.js') as $file)
{
    readfile($file);
}

I am reading the directory as strings because I am doing some parsing with the raw JS (but that is separate from what I am doing here). I know to use php to include a bunch of files by statically coding them in, I want to do it on a by directory basis that is dynamic.

Unless you have some sort of file naming convention going on, remember that the order of the files is going to be basically limited to what you can do with php's array sorting functions on the file names. JS and CSS are often very dependent on code or files being executed or read in a certain order, so depending on what it is you're actually trying to accomplish (still very unclear), this might be an awful idea.


Don't.

Compile your Javascript files into a single, minified source using the Closure compiler, and have your HTML reference that instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜