CSS中的PHP变量不起作用 [英] PHP Variables in CSS not working

查看:81
本文介绍了CSS中的PHP变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html文件具有以下代码:

I have the following code for my html file:

<html>
<head>
<link rel='stylesheet' href='css/style.php' media = "screen"/>
</head>
<body>
<h1 id="foo">My h1 foo element</h1>
</body>
<html>

对于我的php文件:

<?php
    header("Content-type: text/css; charset: UTF-8");
    $asd = '#0000ff';
?>

h1#foo  {
    color: <?php echo $asd;?>;
}

我已经遵循了一些教程,这是我可以做的最简单的教程,但是某种程度上输出结果并没有达到应有的效果.我有什么想念吗?

I've followed some tutorials and this is the simplest one i could make but somehow the output is not working the way it should be. Did i miss anything?

P.S.如果我要在CSS中使用php变量,是否可以动态化?我的意思是在php主体内部,我可以覆盖css中使用的php变量的值,并且输出会改变吗?

P.S. if i was gonna use php variables in css, can it be sort of dynamic? i mean inside the php body, can i overwrite the value of the php variable used in css and the output would change?

非常感谢您的帮助!

推荐答案

对我来说很好^^

使用以下语法:

cssman.php

<?php
ob_clean();
header('Content-Type: text/css');
header('Cache-Control: no-cache, must-revalidate');

error_reporting( 0 );

// These are just to show you can use dynamic values and such:
$type  = isset($_GET['type'])   ? $_GET['type']  : '';
$theme = isset($_GET['theme'])  ? $_GET['theme'] : '';

/** Simply print or echo your css here **/

ob_end_flush(); 
?>

首先尝试浏览输出,方法是手动导航到.php文件.如果根本没有内容,则很可能是PHP代码有错误,为了进行调试,您可以添加错误报告功能(别忘了也要ini_set('display_errors',1),否则错误只会被记录).

Try your output first, by navigating to the .php file manually. If there is no content at all, there is most likely a mistake in the PHP code, for debugging you could add error repporting (don't forget to also ini_set('display_errors',1), else errors will only be logged).

然后将其添加到您的视图中:

Then add it to your view:

您的视图

<style type="text/css">             
    @import "/Library/Stylesheets/cssman.php?type=cms"  screen;
    /* any aditional files here aswell */
</style>

这篇关于CSS中的PHP变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆