在多个 css 个文件中使用 php 个变量
Use php variables in multiple css files
我知道我可以让我的 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">
我的变量。css.php 文件:
<?php
header("Content-type: text/css");
$color = "#004D7F";
?>
我的主要文件。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
,但在服务器端您需要反映实际代码的位置。使用前导正斜杠,服务器正在查找以文件系统的根目录开头的文件。
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 文件一样工作,我已经做到了。我的问题是如何在另一个 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">
我的变量。css.php 文件:
<?php
header("Content-type: text/css");
$color = "#004D7F";
?>
我的主要文件。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
,但在服务器端您需要反映实际代码的位置。使用前导正斜杠,服务器正在查找以文件系统的根目录开头的文件。
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
.