[英]improve and simplify css with php parser
我想以几种方式改进CSS语法:
link-color1 = #fff
width:500-3-2
这看似愚蠢,但带有变量: width: container - inner - 3px;
#foo{background:#ddd;color:#eee;} #bar {@extend: #foo;color:#fff;}
-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;
替换为 border-radius:3px;
这些只是示例,是我的想法,但是库可以不同
我正在寻找一个php库,该库分析语法类似于css的文件,并生成有效的css文档。
这很重要,因为我不想将php sytax插入到我的css文件中,因为:
#foo {color:<?php echo $color1; ?>;}
SASS是脱机工作的非常好的解决方案 ,但是我仍然在搜索PHP解决方案,因为:
这是我写的一个示例 : /css/index.php
php解析器
用法:
@color1: #1a1a1a;
@color2: #444444;
div#container {background:@color1;}
div#inner {background:@color2;}
HTML链接:
<link rel="stylesheet" type="text/css" href="/css/?main.css" />
这仅适用于颜色。 我需要一个列出选项的解析器。
尝试SASS 。 它支持变量,数学计算,样式继承和更多功能。 它不是用PHP实现的,但是会生成静态文件。
您尝试过CSS Crush吗?
您可以通过以下方式将您的CSS与php一起使用:
<link href="style.php" rel="stylesheet" type="text/css" />
在您的style.php中,您可以执行以下操作:
header("Content-Type: text/css");
在这一点上,您可以使用php的所有功能来完成您想做的事情:
<?php
$rules = 'font-size:11px;padding:5px;';
?>
td { <?php echo $rules; ?> }
遗产:
<?php
$parent=array( 'td' => 'color:;padding;etc;'
'p' => 'other rules'
//> other rules
);
?>
.inheritance {
<?php echo $parent['td']; ?>
//> other rules here
}
我不认为这是个好主意,特别是在生产环境中,但是如果您坚持要这样做,我建议您仅安装SASS并使用passthru()从php执行它
http://www.php.net/manual/zh/function.passthru.php
像这样
<?php
$css_path = '/'; // location of your real css files
$sass_cmd = 'sass -f'; // sass cmd
passthru($sass_cmd . $css_path . $_GET['cssfile'];
?>
通过
cssparse.php?cssfile=xyz.css
如果您想花哨的话,可以使用重写规则将所有* .css请求通过PHP脚本集中到一起。
例如:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.css$
RewriteRule ^(.*\.css)$ cssparse.php?cssfile=$1
这样,当您在浏览器中请求/example.css时,它将被重写为/cssparse.php?cssfile=example.css,从而调用SASS并返回输出
您要做的不是一个好主意。 每次用户加载CSS文件时,首先必须通过PHP函数,该函数从“改进”版本生成CSS文件。 例如,仅对于用户可以在其上进行自定义布局的 社交网站才需要。 然后,必须从数据库加载自定义样式,然后使用PHP将其插入CSS文件。
对于没有此功能的网站,在解析CSS之后,没有区别。 对您来说,创建CSS文件只是更加轻松/高效。
另外,如果您的CSS必须通过PHP解析器,则无法离线修改CSS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.