【发布时间】:2015-08-17 11:45:49
【问题描述】:
情况
我将 HTML 和 CSS 与 PHP 变量混合在一起,这样我只需一个配置文件就可以管理大量设置。这一切都很好,但我现在正在尝试合并和缩小 CSS。这会导致问题。
问题
变量不会被回显到压缩表中,因为 PHP 脚本不会被执行。那是因为 file_get_contents() 将内容转换为字符串。
问题
是否可以以某种方式先执行文件,然后获取其内容?或者以另一种方式获取它们的内容,它们仍然会被执行?
文件
config.php
$priColor = '#000';
base-stylesheet.php
/* CSS header defined */
/* config.php included */
body{
background-color: <?= $priColor ?>;
}
specific-stylesheet.php
/* CSS header defined */
/* config.php included */
.site-specific-element{
background-color: <?= $priColor ?>;
}
缩小样式表.php
// Set files that must be minified
$cssFiles = array(
"base-styleseet.php",
"specific-stylesheet.php"
);
// Get those files
$buffer = "";
foreach ($cssFiles as $cssFile) {
$buffer .= file_get_contents($cssFile);
}
// Minify and echo them
minifyCSS($buffer);
echo $buffer;
index.php
<link rel="stylesheet" href="minified-stylesheets.php">
【问题讨论】:
-
$priColor = #000;在#之后不会被注释掉吗? -
在我使用引号的实际文件中没有,在这个例子中忘记了它们。现已编辑。 CSS/PHP 混合使用单个文件。但在合并和压缩它们时不会。
-
好的...感谢您的澄清。
:S -
为什么不使用您希望的任何设置和
include它创建一个关联数组。 -
我在编辑评论时有一点延迟。它有足够的内容成为有用的答案。所以,我用内容回答了这个问题。