php 如何在css文件中使用PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19338116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PHP inside css file
提问by Malintha
I have CSS file and I want to refer some image paths in that files in PHP varaible format. Then I refer that css file inside a html file. Following are my file
我有 CSS 文件,我想以 PHP 可变格式引用该文件中的一些图像路径。然后我在 html 文件中引用该 css 文件。以下是我的文件
CSS file
CSS文件
<? header ("Content-type: text/css");?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
HTML file
HTML文件
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="css/layout.css" media="screen">
</head>
Other things. Can you explain me how to do this ?
其他事情。你能解释一下怎么做吗?
回答by Will Hannah
If you're able to rename your CSS file "layout.php", there's no need for all of these workarounds:
如果您能够将 CSS 文件重命名为“layout.php”,则不需要所有这些解决方法:
Your layout.php file would look like:
您的 layout.php 文件如下所示:
<?php header("Content-type: text/css; charset: UTF-8"); ?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
Your HTML files would look like:
您的 HTML 文件将如下所示:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="css/layout.php" media="screen">
</head>
This question is very similar: Include: css with php file extension?
回答by Spooky
Perhaps the return value of base_url()
does not end in the path separator.
也许 的返回值base_url()
不以路径分隔符结尾。
With that in mind, try this:
考虑到这一点,试试这个:
@import url("<?php echo base_url().'/public/';?>css/layout.css");
(Notice the slash before "public")
(注意“public”前的斜线)
- Check the source of the page via your browser's "view source" or similar, and check if the path in the
@import
is correct
- 通过浏览器的“查看源代码”或类似方式检查页面来源,并检查其中的路径
@import
是否正确
or
或者
- Use a request logger similar to Chrome's devtools' "network" tab to see what URL your browser is trying to load the imported CSS file from.
- 使用类似于 Chrome 的 devtools 的“网络”选项卡的请求记录器来查看您的浏览器试图从哪个 URL 加载导入的 CSS 文件。
You also view the CSS via your browser to identify whether the contents are being correctly built. If you see <?php
inside the response, you'll need to make Apache treat the CSS file as if it was PHP.
您还可以通过浏览器查看 CSS 以确定内容是否正确构建。如果您<?php
在响应中看到,则需要让 Apache 将 CSS 文件视为 PHP。
You can add something similar to the following into your .htaccess file:
您可以在 .htaccess 文件中添加类似于以下内容的内容:
<FilesMatch "\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
You should ensure that the "mod_headers" Apache module is enabled to allow the use of the Header
directive.
您应该确保启用了“mod_headers”Apache 模块以允许使用该Header
指令。
Although, personally I would rename such dynamic stylesheets to have a .php.css extension. This will have no effect, but then Apache can be configured to only pass the dynamic stylesheets to the PHP preprocessor.
虽然,我个人会将此类动态样式表重命名为具有 .php.css 扩展名。这不会有任何影响,但随后可以将 Apache 配置为仅将动态样式表传递给 PHP 预处理器。
<FilesMatch "\.php\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
回答by Joseph
I believe the problem is that a .css
file isn't going to be interpreted as PHP and so your code in the file is not going to be executed. If you included it as a PHP file, your code should be executed and your values should be filled in.
我相信问题在于.css
文件不会被解释为 PHP,因此文件中的代码不会被执行。如果您将其作为 PHP 文件包含在内,则应执行您的代码并填写您的值。
[Edit] This does seem to be the way to do it, as noted by an answer someone linked to in a comment on the original post here.
[编辑] 这似乎是这样做的方法,正如某人在此处对原始帖子的评论中链接到的答案所指出的那样。
回答by whizzzkid
It's simple if you have a bit of knowledge of url rewriting.
如果您对 url 重写有一些了解,这很简单。
Write a .htaccess file in your root directory, It would look something like:
在你的根目录中写一个 .htaccess 文件,它看起来像:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^customized\.css$ css\_generator\.php [L]
</IfModule>
Now just create a file css_generator.php having content:
现在只需创建一个包含内容的文件 css_generator.php:
<?php header('Content-type: text/css; charset: UTF-8'); ?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
Your html should look like:
您的 html 应如下所示:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="customized.css" media="screen">
</head>
Understanding what just happened.
了解刚刚发生的事情。
- On page load when your browser will load customized.css, it will be redirected to css_generator.php
- the contents of css_generator.php will be also available as yoursite.com/customized.css
- 当您的浏览器加载customized.css 时,页面加载时,它将被重定向到css_generator.php
- css_generator.php 的内容也将作为 yoursite.com/customized.css 提供
Hope this helps
希望这可以帮助