Jump to content

including php constants in css file - help!


Kingskin

Recommended Posts

OK good people of php freaks, here's one for you:

I have an external stylesheet that I want to include php a php constant in (the constant is a url, idea being that making themes is easy - i just set the constant to the location of my theme and bingo).

To do this I'm saving my css as a .php file with the following code at the top:

[code]header('Content-type: text/css');[/code]

I then include my constants file, and then the stylesheet like this:

[code]include_once("includes/define.php");
...
<link href="css/main.php" rel="stylesheet" type="text/css" />[/code]

that works fine, my css is displayed as it should be, except that my defined constant is not available within the main.php css file. I can echo it out fine above or below the style sheet link and i get the value, but within that script itself it's empty. I can define the same constant within the css script and it all works fine, but the idea was the contant could be set via a database, so there would be no need to edit any source code to change themes.

I then thought if I just run the query to define the constant inside the css script, that would work fine. I tried that, but it won't work - no error message, just an empty result.

So, I'm at a loss. Anyone know why this is happening and how I can get round it?

Cheers.
Link to comment
Share on other sites

That's an interesting way to do it. I'm sure I'm doing it incorrectly, but I just include_once("css.php") rather than trying to use <link ....> My way of doing it displays the CSS on the current page rather than "including" it as a link referance. Thus, it's a little sloppy. But it works :)
Link to comment
Share on other sites

[quote author=kalivos link=topic=101405.msg401309#msg401309 date=1153503261]
That's an interesting way to do it. I'm sure I'm doing it incorrectly, but I just include_once("css.php") rather than trying to use <link ....> My way of doing it displays the CSS on the current page rather than "including" it as a link referance. Thus, it's a little sloppy. But it works :)
[/quote]

and are you using php constants or variables inside your css? I'd really like to keep the css separate if I can though.
Link to comment
Share on other sites

I use both, depending on which site I refer too  ;D I'm currently making a site with user switchable themes and styles. I'm trying out a new approach of using $_SESSION(). This is my way of trying out different formats.

[code]<link href="<? echo $_SESSION['theme']; ?>/css/main.css" rel="stylesheet" type="text/css" />[/code]

This way the CSS is constant and user selects the theme. Experiment and have fun  :D
Link to comment
Share on other sites

well the thing is, your HTML would access the CSS via HTTP as far as i know and PHP would have parsed the main.php file via Apache by then.

What you could do is pass the variables to CSS via the URL:

main.php
[code=php:0]
<?php
header('Content-type: text/css');
?>
body {background: #<?php echo $_GET['bg']; ?>;}
[/code]


HTML file
[code=php:0]
<link href="main.php?bg=0f0" rel="stylesheet" type="text/css" />
[/code]
Link to comment
Share on other sites

[quote author=kalivos link=topic=101405.msg401353#msg401353 date=1153508313]
I use both, depending on which site I refer too  ;D I'm currently making a site with user switchable themes and styles. I'm trying out a new approach of using $_SESSION(). This is my way of trying out different formats.

[code]<link href="<? echo $_SESSION['theme']; ?>/css/main.css" rel="stylesheet" type="text/css" />[/code]

This way the CSS is constant and user selects the theme. Experiment and have fun  :D
[/quote]

OK, that's a slightly different interpretation of the thing i'm trying - you have a css file for each theme which is something I need to incorperate. A basic one for the regular, theme-independant one and then the one for the theme. The way you are doing things was what I was going to do if I couldn't get it working this way, but once I try something and it doesn't work I like to know why!

[quote author=Koobi link=topic=101405.msg401367#msg401367 date=1153509716]
well the thing is, your HTML would access the CSS via HTTP as far as i know and PHP would have parsed the main.php file via Apache by then.

What you could do is pass the variables to CSS via the URL:

main.php
[code=php:0]
<?php
header('Content-type: text/css');
?>
body {background: #<?php echo $_GET['bg']; ?>;}
[/code]


HTML file
[code=php:0]
<link href="main.php?bg=0f0" rel="stylesheet" type="text/css" />
[/code]
[/quote]

[quote author=ryanlwh link=topic=101405.msg401436#msg401436 date=1153520271]
by using LINK, main.php is treated as a separated script and would not see the variable scope of the current page as Koobi mentioned. you could use Koobi's methed, or include define.php within main.php
[/quote]

Yup, perfect thanks. I passed the constant to main.php via the url and it worked perfectly. Thanks for the help.
Link to comment
Share on other sites

[quote author=ryanlwh link=topic=101405.msg401436#msg401436 date=1153520271]
by using LINK, main.php is treated as a separated script and would not see the variable scope of the current page as Koobi mentioned. you could use Koobi's methed, or include define.php within main.php
[/quote]
i don't know why i didn't think to include define.php in main.php
i think that would be a better solution than passing the values via the URL.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.