我知道我可以让我的 php 文件像 css 文件一样运行,我已经做到了。我的问题是如何在另一个 css-php 文件中使用变量?我的带有全局变量的 css.php 文件在 html 列表中先于其他文件加载,但它似乎不包含它。
我的 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">
我的 variables.css.php 文件:
<?php
header("Content-type: text/css");
$color = "#004D7F";
?>
我的 main.css.php 文件:
<?php header("Content-type: text/css"); ?>
body {
background: <?=$color;?>;
}
但是它不起作用,它不识别变量$color。所以我尝试在 main.css.php 中添加:
require("/assets/css/variables.css.php");
但是它抛出了一个错误。有人知道答案吗?如何在另一个充当 css 的 php 文件中访问 php 变量?
提前致谢!
-- 卡洛斯
最佳答案
Web 服务器看到的不是浏览器看到的。具体来说,虽然文件的路径是 http://domain/assets/css/variables.css.php
,但在服务器端您需要反射(reflect)实际代码的位置。使用前导正斜杠,服务器正在寻找从文件系统的根目录开始的文件。
Remove the leading slash
如果相对路径正确,这可能足以使其工作
如果还是不行,
Remove parts of the path until it works
如果两个文件都位于 /var/www/html/assets/css/
中,那么只需执行 require("variables.css.php")
就足够了.
此外,无需在网页中包含 variables.css.php
。 PHP 将在 main.css.php
中自动填充值。
关于php - 在多个 css 文件中使用 php 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394403/