简体   繁体   中英

import whole files in the Directory

I need to import whole css files available in a specific folder (css) Is there a method to do it without importing file by file?

need to import assets/css/* ?

    <link href="<?php echo base_url(); ?>assets/css/*" rel="stylesheet"/>
        <title>Untitled Document</title>
        <style>
            #area {
                max-width: 600px;
            }
        </style>


</head>

Edit: Late answer it seems and you have accepted an answer. Had I known, I would not have submitted this. I was busy testing and formulating it.

As I stated in a comment, you can use PHP's glob() function to achieve this, along with a foreach loop. Here is what you can do:

foreach (glob("*.css") as $css) {
    echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"$css\">\n";
}

Which in HTML source will produce something similar to:

<link type="text/css" rel="stylesheet" href="css_1.css">
<link type="text/css" rel="stylesheet" href="css_2.css">
<link type="text/css" rel="stylesheet" href="css_3.css">

Depending on where your files are located, you may need to change the path to those files.

Ie:

foreach (glob("path/to/css/folder/*.css")

Placing the PHP for it inside <head></head>

For example:

<!DOCTYPE html>

<head>

<?php 

    foreach (glob("*.css") as $css) {
        echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"$css\">\n";
    }

?>

</head>

<body>

    <div id="css_1">Style 1</div>
    <br>
    <div id="css_2">Style 2</div>
    <br>
    <div id="css_3">Style 3</div>

</body>
</html>

I think there is no way you can import css files from a folder by doing assets/css/* . But you can concatenate files from css folder into one file and then minify this css file and import it.

You can use build tools like gulp to accomplish this thing. Check this out https://www.npmjs.com/package/gulp-minify-css

No. For one thing, the client would have no idea what to request (it can request only one file per http request, whose name it needs to know). You can do it in four ways:

  • Concatenate all of your CSS files into one file, then link to that file.
  • Have a script that will concatenate all CSS files into one response, then link to that script
  • Have a loop in your template going over all the CSS files, which will issue one <link> element per file.
  • If you only have several CSS files than do not change, you can just write them all out by hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM