CSS预处理器还是PHP? [英] CSS Preprocessor or PHP?

查看:70
本文介绍了CSS预处理器还是PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用PHP编写代码,是否有理由为什么我要使用CSS预处理程序而不是PHP?例如,可以通过在标头中包含以下内容来在CSS文件中使用PHP:

If I'm writing 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 by having this in my header:

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

这样,我可以将像style.php?color=#000

或者我可以使用 LESS 之类的东西来预处理CSS.如果我使用less.js,则不确定如上例中那样如何传递变量.

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

现在,我听说无法缓存PHP CSS文件,因此我可以知道为什么会出现问题,尤其是在CSS文件很大的情况下.但我希望能够将变量传递到CSS工作表.

Now, I've heard that PHP CSS files can't be cached so I can see why that would be a problem, especially if the CSS file was large. But I'd like the ability to pass variables to my CSS sheet.

有人可以告诉我更多为什么我要使用另一个,以及/或者如果我使用less.js时如何将变量传递到.less文件?

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

推荐答案

现在,我听说无法缓存PHP CSS文件,所以我知道了为什么会出现问题,尤其是在CSS文件很大的情况下.

Now, I've heard that PHP CSS files can't be cached so I can see why that would be a problem, especially if the CSS file was large.

可以缓存

PHP CSS文件 ,但是如果将动态值传递给它们,则通常会丢失缓存点.如果您的动态值可能随每个请求而变化,则缓存将变得毫无意义.

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

此外,通过PHP预处理程序推销大量的大多数静态CSS往往浪费服务器资源.

Also, shoving huge amounts of mostly static CSS through the PHP preprocessor tends to be a waste of server resources.

更简单的方法通常是拥有静态CSS文件,并在页面主体中声明所有动态值:

The much easier approach is usually to have static CSS files, and to declare all dynamic values in the page body:

<!-- 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>

这样,您可以随意获取动态值,但仍避免使用PHP处理大量CSS数据.

This way, you can have dynamic values as you please, but you still avoid having a lot of CSS data being processed by PHP.

这篇关于CSS预处理器还是PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆