Carlos2W
Carlos2W

Reputation: 2544

Use php variables in multiple css files

I know I can ask my php file to act like a css one, I already did. My problem is how to use variables in another css-php files ? My css.php file with global variables is loaded before others in html list, but it doesn't seem it includes it.

My html :

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

My variables.css.php file :

 <?php
   header("Content-type: text/css");
   $color = "#004D7F";
 ?>

My main.css.php file :

<?php header("Content-type: text/css");  ?>
body {
    background: <?=$color;?>;
}

But it doesn't work, it doesn't recognize the variable $color. So I tried to add at main.css.php :

require("/assets/css/variables.css.php");

But it threw an error. Does someone know the answer ? How to get access php variables in another php file acting as css ?

Thank you in advance !

-- Carlos

Upvotes: 2

Views: 442

Answers (1)

Anonymous
Anonymous

Reputation: 12090

What the web server sees is not what the browser sees. Specifically, while the path to the file would be http://domain/assets/css/variables.css.php, on the server side you need to reflect the location the actual code. With the leading forward slash, the server is looking for the file starting with the root of the filesystem.

Remove the leading slash

That may be enough to make it work if the relative path is correct

If that doesn't help,

Remove parts of the path until it works

If both files are sitting in /var/www/html/assets/css/, then just doing require("variables.css.php") is enough.


Additionally, there's no need to include the variables.css.php inside the webpage. PHP will populate the values automatically inside main.css.php.

Upvotes: 1

Related Questions