CSS or PHP preprocessor?

If I write code in PHP, is there a reason why I would use a CSS preprocessor instead of PHP? For example, I could use PHP in my CSS file, having this in my header:

<link rel="stylesheet" type="text/css" media="all" href="style.php" />

That way I could pass variables like style.php?color=#000

Or I could use something like LESS to preprocess my CSS. If I use less.js, I'm not sure how I can pass variables, as in the previous example.

Now I heard that PHP CSS files cannot be cached, so I can understand why this was a problem, especially if the CSS file was large. But I would like to pass the variables to my CSS sheet.

Can someone tell me a little more about why I will use one above the other, and / or how to pass variables to my .less file if I used less.js?

+5
source share
3 answers

Now I heard that PHP CSS files cannot be cached, so I can understand why this was a problem, especially if the CSS file was large.

PHP CSS files can be cached, but if you pass them dynamic values, the cache point is usually lost. If you have a dynamic value that can change with each request, caching becomes pointless.

In addition, dragging huge amounts of mostly static CSS through a PHP preprocessor is usually a waste of server resources.

- , , CSS :

<!-- the static style sheet declares all the static values --> 
<link rel="stylesheet" type="text/css" href="static.css"> 
<!-- now you override all the dynamic values -->
<style>
  .classname { color: <?php echo $color; ?> }
</style>

, , , CSS, PHP.

+4

HTTP- , . rfc2616.

, , , GET , , PHP . .

css :

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

: http://css-tricks.com/snippets/php/intelligent-php-cache-control/

+3

, :

Static CSS files can be cached into memory (and even pre-compressed with some servers, such as nginx), which allows you to serve them from a static domain that does not support cookies. Using a web server such as nginx can create a huge performance boost because less RAM is used. If you don’t have a lot of RAM or a lot of traffic, the difference can be huge.

If you have a small website, this is not a big deal.

+1
source

All Articles