简体   繁体   English

使用PHP在我的CSS样式表中创建变量有什么缺点?

[英]What are the drawbacks of using PHP to create variables in my CSS stylesheet?

One significant drawback of CSS is that one can't use variables. CSS的一个重要缺点是不能使用变量。 For example, I'd like to use variables to control the location of imported CSS, and it would be awesome to create variables for colors that are used repeatedly in a design. 例如,我想使用变量来控制导入的CSS的位置,为设计中重复使用的颜色创建变量会很棒。

One approach is to use a PHP file for the CSS stylesheet. 一种方法是将CSS文件用于CSS样式表。 In other words, create a "style.php" with... 换句话说,用...创建一个“style.php”

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

...at the top of the file, and then link to it using... ...在文件的顶部,然后使用...链接到它

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

...in any file that uses these styles. ...在任何使用这些样式的文件中。

So what's the catch? 那捕获的是什么? I think it might be performance -- I did a few quick experiments in Firefox/Firebug and as one would expect, the CSS stylesheet is cached, but the PHP stylesheet isn't. 我认为它可能是性能 - 我在Firefox / Firebug中进行了一些快速实验,正如人们所预料的那样,CSS样式表被缓存,但PHP样式表却没有。 So we're paying the price of an additional GET. 所以我们要付出额外的GET的代价。

The other annoying thing is that TextMate does not syntax highlight properly for CSS in a .php file. 另一个令人讨厌的事情是,TextMate没有为.php文件中的CSS正确地突出语法。

Are there other drawbacks? 还有其他缺点吗? Have you used this approach, and if so, would you recommend it? 您是否使用过这种方法?如果是这样,您会推荐它吗?

Performance is pretty much it. 性能几乎就是这样。 It's a good idea, but only if you cache it. 这是一个好主意,但只有你缓存它。 You can send out browser headers to ask the client to pretty-please cache it, but if performance is an issue, you might benefit from developing a system through which you compile your PHP-enabled stylesheets to vanilla CSS files to serve as normal. 您可以发送浏览器标题以请求客户端非常好地缓存它,但如果性能是一个问题,您可能会从开发一个系统中受益,通过该系统将启用PHP的样式表编译为vanilla CSS文件以正常运行。

If you're going to bother to hand-roll your own compilation system, though, you might want to look into SASS , instead. 但是,如果您打算手动滚动自己的编译系统,则可能需要查看SASS

You should still be able to set the appropriate HTTP headers to instruct the browser to cache the dynamically generated CSS. 您仍应该能够设置适当的HTTP标头,以指示浏览器缓存动态生成的CSS。 You may be interested in checking out the following Google Code article for further reading on the topic: 您可能有兴趣查看以下Google Code文章,以便进一步阅读相关主题:

You could also consider generating a static CSS file from your script, and then include that from your web document. 您还可以考虑从脚本生成静态CSS文件,然后从Web文档中包含该文件。 This eliminates realtime preprocessing and any performance concerns related to that, at the cost of having to "compile" your CSS files whenever you change them. 这消除了实时预处理以及与此相关的任何性能问题,代价是在您更改CSS文件时必须“编译”它们。 However if you are already minifying CSS or JavaScript, you could simply add this extra step to your build process. 但是,如果您已经在缩小 CSS或JavaScript,则只需在构建过程中添加此额外步骤即可。

As for code highlighting, you may want to use normal CSS files with variables in them instead of hardcoded constants. 至于代码突出显示,您可能希望使用包含变量的普通CSS文件而不是硬编码常量。 Then your php preprocessor can load the CSS file and substitutes the variables for the actual values. 然后你的php预处理器可以加载CSS文件并用变量替换实际值。

The drawback is that the file is not cached (like you pointed out) as well as the fact that the server must compute the CSS file for EACH request. 缺点是文件没有缓存(就像你指出的那样)以及服务器必须为EACH请求计算CSS文件的事实。

Loading the static file is pretty much no load for the server as it just reads and dumps the file, but for the PHP script it will have to execute it for each page request, which might add additional overhead. 加载静态文件对于服务器来说几乎没有负担,因为它只是读取和转储文件,但对于PHP脚本,它必须为每个页面请求执行它,这可能会增加额外的开销。

You could possibly cache the CSS in memory or Memcache or something, but that will still not be a efficient as just using a static file. 您可以将CSS缓存在内存或Memcache等中,但这仍然不会像使用静态文件那样高效。

Why not define the majority of your CSS in a static file, and then just override the specific styles that change? 为什么不在静态文件中定义大部分CSS,然后只是覆盖更改的特定样式?

Not exactly an answer, but an addition to @Matchu answer. 不完全是答案,但是@Matchu答案的补充。
Here is a code sni[et I used several years ago, You can start play from that as base to develop your own method of client side caching rules. 这是一个代码sni [我几年前使用过,你可以从它开始玩作为基础来开发自己的客户端缓存规则方法。 Anybody who feels he can improve that, be welcome. 任何觉得自己可以改进的人都会受到欢迎。

<?php

//functions to cache HTML output Or JS/CSS output from a PHP script

class ControlHtmlCache

{

    //Will cache output of a php script on the browser for the giver hours.

    //Do notice, this will not cahce from now until now+hours, but rather for a rounded time period in the time stamp

    //For example, If I send 4 it will refresh the cache at approx 3,7,11,15,19,23 (In the summer, it will be 4,8,12....)

    static function client_side_cache($hours)

    {

        //in the event a session start is used, I have to clean all the #$%# headers it sends to prevent caching

        header('Cache-Control: ',true);

        header("Pragma: ", true);

        header("Expires: ", true);



        //get the If-Modified-Since header in a unix time format

        $headers = getallheaders();

        if (isset($headers['If-Modified-Since']))

        {

            $modifiedSince = explode(';', $headers['If-Modified-Since']);

            $modifiedSince = strtotime($modifiedSince[0]);

        }

        else

        {

            $modifiedSince = 0;

        }



        //calculate the Last-Modified timestamp

        $current_time=time();

        $last_modified=($current_time)/($hours*3600);

        $last_modified=(int)$last_modified;

        $last_modified=$last_modified*$hours*3600;



        //check cache not expires

        if ($last_modified <= $modifiedSince)

        {

            header('HTTP/1.1 304 Not Modified');

            exit();

        }

        else //emit a new Last-Modified (either cache expired or page wasn'r cached

        {

            Header('Last-Modified: '.gmdate("D, d M Y H:i:s",$last_modified).' GMT ');

        }

    }

}//EOF class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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