Forum Moderators: coopster
I want to do the same for my CSS. I need mondo comments in my CSS files, and it is supremely awkward to maintain dual copies ("source," and "compiled."). I just want the server to do the same thing to the CSS that it does to my PHP output.
In order to do this, I need to execute the CSS files as PHP. I have tried making the files .php files, but the browser refuses to recognize their CSS-ness, even though I output the correct header. I think the link fails.
What is the best way to have the server execute a PHP file and render it into a stream of pure CSS? I have tried changing the handler for the .css type to PHP, but that doesn't work.
I don't have access to httpd.conf. I must use a .htaccess file. The environment is Apache.
Any ideas?
<? header('Content-type: text/css');?>
<!-- css goes here -->
Then save the file as a php file, and use it with:
<link rel="stylesheet" href="somefile.php" />
There was a thread on this awhile ago, can't seem to find it right off the bat though ...
-sned
<?php header('Content-Type: text/css'); ?> Otherwise you risk serving the file as
text/html instead, which will fail in most circumstances. The most important problem you need to address when parsing CSS files with PHP is caching. If you don't set cache headers as well as the above, then your CSS file will be called from the server for each and every page view, thus negating the advantage of an external stylesheet. If you don't cache the file, you might as well just add the CSS inline.
When doing this, I often simply cache the CSS for one hour, you don't need to worry about user agents other than standard browsers as almost all of them won't fetch the CSS file anyway.
If you don't set cache headers as well as the above, then your CSS file will be called from the server for each and every page view, thus negating the advantage of an external stylesheet. If you don't cache the file, you might as well just add the CSS inline.
Can I get an example? I haven't done this with CSS yet, so I could use all the help I can get. The whole idea is to increase performance.
Server Side Scripting in CSS Files [webmasterworld.com]
The <link> tag had type="text/css" in it that seemed to override the PHP, so the whole file was simply read in.
I removed that, and the PHP executes fine.
I still need to examine the headers. At the moment, the only header I send is Content-type: text/css.
I need to think about the caching.
I will say it has already made a pretty big dent in the page load times.
The <link> tag had type="text/css" in it that seemed to override the PHP, so the whole file was simply read in.
Something's not right then. A file that's parsed as a php file should be parsed before the file is served. Nothing that happens in the browser--including the request--can have any influence on this.
I would guess that something else changed at the same time as you removed the 'type' attribute from the link element (i.e. so that the file started to be correctly parsed).
A quick test here shows that it works when the correct 'type' attribute is included.
-b