Forum Moderators: coopster

Message Too Old, No Replies

PHP inside CSS

I can't get PHP to parse inside of CSS files

         

mek2600

7:30 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



I'd like to be able to read variables from a file, then echo them into a CSS file so that my users can have customizable colors, etc, but I'm not getting any PHP in the CSS files to parse.

Is there any special method that I have to use to get that to work? All of my variables are in a file that's being included before the CSS page is being referenced lije this:

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

But I'm also not able to parse ANY PHP in that CSS file, so I'm not sure how to set this up.

Thanks a lot in advance for any suggestions.
MK

coopster

7:32 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have a look at this thread:
Server Side Scripting in CSS Files [webmasterworld.com]

jatar_k

7:35 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



funny how this link isn't in that thread
Apache AddType directive [httpd.apache.org]

uncle_bob

7:36 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



I guess php tags were not being processed because the webserver did not associate a .css file with the php script engine.

The simplest way is to save the css file as
stylesheet.php and change the link to

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

Alternatively you could solve that by telling your webserver that .css files are infact php scripts, but how you do that depends on what webserver you are using. I'd stick to the first method, as it's simple.

mek2600

7:51 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



Wow, you guys are quick. :)

Sorry I missed that thread on this board. I'll check that out now. Thanks.

Also, I tried making the .css into a .php but didn't have any luck. I'll try that again just to see if I missed something. Also, I'm running Apache 2.x, so I probably will make it so that .css get parsed by PHP in the near future.

Thanks again guys.

mek2600

9:18 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



OK, I got the PHP to parse inside the CSS file, so now I have another problem. I'm not able to access any PHP variables from inside the CSS file. Here's my (simplified) code:

<head>
<? $family = "Comic Sans MS";?>
<link href="stylesheet.php" rel="stylesheet" type="text/css" />

Now, if I have this line in stylesheet.php:

p { font-family: <? echo $family;?>; }

It's not changing anything to "Comic Sans MS". I can see that the $family varialbe isn't making it into the CSS page. How do I fix this? In reality I want to include a file full of variables and not just that one line, but once I get this figured out I guess the file will be a cinch to get working.

uncle_bob

2:22 am on Dec 30, 2003 (gmt 0)

10+ Year Member



<? $family = "Comic Sans MS";?>
<link href="stylesheet.php" rel="stylesheet" type="text/css"/>

The stylesheet.php is fetched seperately by the browser, so there is no way for it to access variables that were set in a different page on the server. You need to put the

<? $family = "Comic Sans MS";?>

line into the stylesheet.php file, in order for it to work.

mek2600

2:35 am on Dec 30, 2003 (gmt 0)

10+ Year Member



Yeah, I also found out that I can use this method, too:

<? $family = "Comic Sans MS";?>
<link href="stylesheet.php?family=<?=$family;?>" rel="stylesheet" type="text/css"/>

I don't care for it, but I guess I'll just pass a variable to the CSS page that tells me where the file is at and include it that way. If it's the only way, then I'll have to live with it. :)

Thanks.

ruserious

8:52 am on Dec 30, 2003 (gmt 0)

10+ Year Member



What is the actual "problem" you are trying to solve?
Maybe there is an easier way to solve it?