【问题标题】:Use php variables in multiple css files在多个 css 文件中使用 php 变量
【发布时间】:2015-10-02 08:51:03
【问题描述】:

我知道我可以让我的 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 变量?

提前谢谢你!

-- 卡洛斯

【问题讨论】:

  • 你能告诉我们require产生的错误吗?请
  • 我不能,因为它是一个 css 文件
  • 还有几个问题。您是如何启动 .html 文件的?您是使用 apache(例如 localhost/test.html)还是通过双击文件直接启动?如果你通过双击启动,它不能工作,因为你的 .php 文件不能被执行。
  • 当我说 apache 时,我的意思是你的本地网络服务器对不起。

标签: php css variables include


【解决方案1】:

Web 服务器看到的不是浏览器看到的。具体来说,虽然文件的路径是http://domain/assets/css/variables.css.php,但在服务器端,您需要反映实际代码的位置。使用前导正斜杠,服务器正在查找从文件系统的根目录开始的文件。

去掉前面的斜杠

如果相对路径正确,这可能足以使其工作

如果这没有帮助,

删除部分路径直到它起作用

如果两个文件都在/var/www/html/assets/css/ 中,那么只需执行require("variables.css.php") 就足够了。


此外,无需在网页中包含variables.css.php。 PHP 将自动填充 main.css.php 中的值。

【讨论】:

  • 谢谢,你是对的。我最后只是写了 require("variables.css.php") 并删除了 html 页面中的包含,它就可以工作了。
猜你喜欢
  • 2015-09-21
  • 2010-09-27
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2010-09-08
  • 2016-02-15
  • 2018-03-13
相关资源
最近更新 更多