简体   繁体   中英

Passing data to dynamic CSS file?

This is more a question of what browsers will do, rather than the back-end architecture.

I am using codeigniter and have a controller setup to handle assets (CSS, JS, and images), and at the moment I am simply processing the PHP out of CSS files and presenting them as CSS files to the browser, so www.mysite.com/asset/home.css will call the asset class and generate the CSS file for home from a single file.

I would like to make the request for the CSS files more dynamic, so that the request will determine multiple files that should be combined, and then passed to less.php for parsing and minimization.

Taking into account issues of caching, what would be the best method of passing variables to the CSS class? Flat Link URI variables? Traditional GET URI? I could use a database to reference a given name for its components, but isn't that a lot of overhead?

Any thoughts or opinions are welcomed!

++ How would browsers handle something like standard.menu.comments.css ?

+++ I ended up going with a URI string appended to the file. It's not as clean I would want, but it appears to be working. I will likely move to a flat slash separated URI parser soon to clean up the request lines. Thanks for your help!

You can create a file style.php with the following header:

<?php header("Content-type: text/css"); ?>

And link this style in your template:

<link rel="stylesheet" type="text/css" media="screen" href="style.php?color=red">

Then you can import some stylesheets in your style.php :

@import ("someStyle.css");

Or you can pass some $_GET variables and create conditional styles:

<?php 
    if ($_GET['color'] == 'red')
    {
        echo ".myBlock {background: #ff0000;}";
    } 
    else
    {
        echo ".myBlock {background: #00ff00;}";
    }
?>

If you just don't want your .css files to be cached, append random $_GET variable to your linked style:

<link rel="stylesheet" type="text/css" media="screen" href="style.css?<?php echo time(); ?>">

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