Forum Moderators: coopster

Message Too Old, No Replies

Reading CSS Files as Executable PHP

I need smart CSS files. How can I make Apache do it?

         

cmarshall

5:59 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a "page compressor" for my PHP. This strips out extra whitespace and HTML comments, and delivers a really ugly, but fast HTML page. I use the ob_ functions and RegEx to do it.

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?

sned

6:03 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



A quick way to do something like this is with the header function:

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

coopster

6:05 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Use an AddHandler
AddHandler php5-script css

encyclo

6:26 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the following is vital in the file however you try to handle it:

<?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.

cmarshall

6:26 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks!

I'll work on these later (I'm at my "Day Job"), and see how it goes.

cmarshall

6:30 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

jatar_k

8:30 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this thread from the CSS Library [webmasterworld.com] might be useful as well

Server Side Scripting in CSS Files [webmasterworld.com]

cmarshall

2:28 am on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got it. Silly boneheaded mistake (they always are).

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.

bedlam

3:30 am on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

cmarshall

4:15 am on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point. I'll check it out.