Forum Moderators: coopster

Message Too Old, No Replies

PHP in CSS files

can it be done?

         

benlieb

4:50 am on Nov 9, 2004 (gmt 0)

10+ Year Member



I've frequently wanted to use some form of variable in my external CSS files. For example in a given color scheme you may use one color in 20 places, then want to change that color everywhere quickly.

Alas, I don't think scripting is allowed in CSS files, or is there some way I don't know of?

I've thought of just putting the CSS in the page itself, thereby getting PHP in on the action. But this seems like a step in the wrong direction.

bobnew32

5:12 am on Nov 9, 2004 (gmt 0)

10+ Year Member



Hmm would this work, add this to your .htaccess file:


AddType application/x-httpd-php .css


It should work me thinks. You can still use it as css files and the browser will think they are still normal, but you can insert <?php?> in there :)

jollymcfats

5:19 am on Nov 9, 2004 (gmt 0)

10+ Year Member



This technique works great, and I use it all the time.

Just remember to output the proper MIME type at the top of your PHP css file:

<? header('Content-Type: text/css');?>

If your generated CSS won't be changing from request to request, I'd also suggest finding and using a recipe for handling conditional "If-Modified-Since" browser requests. Otherwise some browsers will be re-requesting the CSS more often than they need to.

Zipper

5:34 am on Nov 9, 2004 (gmt 0)

10+ Year Member



I second that. Create a new php file. Organize ur styles in some what the following way.

<?php
header('Content-type: text/css');
$col1 = "#CCC";
echo ("
.ex {
color:$col1;
}
#ex2{
bgcolor:$col2;
}
");
?>

Having done that, you can call in the normal way.


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

But remember, php >> css means more work on the server for every page view. Hope it helps.

Salsa

7:57 am on Nov 9, 2004 (gmt 0)

10+ Year Member



Yes, it was a super revelation for me. I set up to parse .css files some time ago, and I now include a customized config file into a template .css/PHP file for all of my clients.

Zipper's comment on server load is very worthy, however, and I've been considering converting to a script that writes .css flat files instead (it's not like you want CSS files dynamic anyway!). So before becoming too invested, consider that option.

As it is, in the config file I like to define constants rather than setting variables for colors, etc. That way if I need some odd-ball style attribute in a span tag on other pages I can easily pop it in wherever I want.

mincklerstraat

8:14 am on Nov 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



check out also the possibility of putting some kind of Last-modified, Expires, or Cache-control with an expiry time on it with header(), otherwise your css probably won't be cached by the browser.

benlieb

4:48 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



I've tried adding

AddType application/x-httpd-php .css

to my .htaccess file &

header('Content-Type: text/css');

to the top of the php in the .css file...

and it still won't parse the .css file with php. I know this because I requested the .css from the browser and it (both moz & ie) didn't know what to do with it. When I finally saw it, the php was intact, unparsed. Now what?

mincklerstraat

4:55 pm on Nov 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you have the Content-type header in place, you don't need the .css extension any more really - just change it back to a .php file and see then how it works.

benlieb

5:09 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



I've changed the .css to .php and that works.

However I also removed the

AddType application/x-httpd-php .css

from the .htaccess file and it still works. I thought the purpose of the above code was to get php to parse inside css files. Why won't this work if I leave the .css ext on?

What this means to me is that since I have many sites on one server, whenever I want to use php in a .css file, I have to rename it. What I would prefer is to put that code in my .htaccess file and just use php in the .css files as needed (which may be never). What am I doing wrong?

p.s. thanks for the help :)

Salsa

8:26 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



If you have access to your httpd.conf file, try adding it into the VirtualHosts that need/want it. That's the only way I've ever done it, and it worked the first time. My typical setup is:

<VirtualHost ...>
...
AddType application/x-httpd-php .htm .css
...
</VirtualHost>

Oh, yeah. At least when you do it that way you have to restart Apache, though.

Grimdotdotdot

4:56 pm on Dec 1, 2004 (gmt 0)



Hi there.

Here is a quick script I wrote that creates a CSS file (load your CSS from your DB into a string called '$css' first). Make a blank file called 'blank.css'.


<?php
//create file
$filename = "test.css";
$blankfile = "blank.css";
unlink ($filename);
if (!copy($blankfile, $filename))
{
echo "Can't copy!";
}
if (is_writable($filename))
{
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $css) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
?>