简体   繁体   English

使用PHP生成CSS,是/否?

[英]Generate CSS with PHP, yes/no?

Is there any reason you should not use PHP to overcome some of the shortcomings of CSS? 有什么理由你不应该使用PHP来克服CSS的一些缺点? Live inspection would be harder but are there any functional downsides? 实时检查会更难,但有任何功能上的缺点吗?

Yes, it's great to preprocess CSS somehow. 是的,以某种方式预处理CSS非常棒。 But do not invent a wheel. 但是不要发明轮子。 There are lots of "standard" options to use: 有许多“标准”选项可供使用:

The only downside I can think of is server processing power and lack of easy caching of CSS files. 我能想到的唯一缺点是服务器处理能力和CSS文件缺乏简单缓存。

Other then that (both can be remedied) it's perfectly acceptable. 除此之外(两者都可以补救)它是完全可以接受的。

Have you looked at one of the precompiled css languages like LessCss or Sass? 你有没有看过像LessCss或Sass这样的预编译css语言?

http://lesscss.org/ http://lesscss.org/

http://sass-lang.com/ http://sass-lang.com/

It's really a matter of preference as well as the requirements of your specific situation. 这确实是一个偏好问题,也是您特定情况的要求。 The main argument against mixing programming with CSS is that it complicates something that was specifically designed for non-programmers to be able to do. 反对将编程与CSS混合的主要论点是,它使专门为非程序员设计的内容变得复杂。

If you use php within css you've limited that file to run only through php so you lose compatibility. 如果你在css中使用php你已经限制该文件只能通过php运行,所以你失去了兼容性。

There are plenty of tools out like SASS that are designed for making css more powerful. 有很多像SASS这样的工具可以让css更强大。

Personally I like to put color values into php variables so I can be sure to get them right and so I can re-skin later. 就个人而言,我喜欢将颜色值放入php变量,所以我可以确保它们正确,所以我可以稍后重新皮肤。 Other than that I keep it clean. 除此之外,我保持干净。

I think it is a fine idea, and if you know PHP already it will be easier than learning a new syntax, such as sass/less, etc. 我认为这是一个好主意,如果你已经知道PHP,它将比学习新的语法更容易,比如sass / less等。

Unless it is a toy site I would not recommend creating the css on the fly with each request. 除非它是一个玩具网站,否则我不建议在每次请求时动态创建css。 Instead create a script that builds the css files from templates every time the server starts up, or to a schedule most useful to you. 而是创建一个脚本,每次服务器启动时从模板构建css文件,或者创建对您最有用的计划。

More sophisticated still would be using something like make to only build them when the input templates have changed. 更复杂的仍然是使用像make这样的东西,只在输入模板发生变化时才构建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM