2

I am looking for an implementation of a CSS generator in PHP. The idea is that the script gets some input like : array('element' => 'div', 'color' => '#00ff00') and generates a file with :

div {
  color : #00ff00; 
} 

Or something similar, you get the idea. Please do not point to some software or online service, I am looking for an actual open source implementation that I can possibly use in my projects, like a PHP class or so.

Is there an open source implementation of that which you know of ?

Spyros
  • 46,820
  • 25
  • 86
  • 129

1 Answers1

2

LESS or SASS...both free, open source. They will provide at least a good starting point for CSS parsing and templating logic (even if it may not be the right solution-see comments).

  • LESS - JavaScript, C#, PHP versions exist (maybe others).
  • SASS - Ruby, JavaScript, and PHP

Unlike some of the comments state, this isn't a trivial task if you want the tool to be useful (I'm assuming your code example is simplified from a full implementation).

There are a couple of ways to implement CSS pre-processing:

  • In the browser (the JS versions). This puts a lot of load on the browser with every page view and can cause issues.

  • On the server. This approach usually generates then caches the output, so it's acceptably fast. I like this for development work because I can see my changes in real time.

  • Pre-compilation. With this approach, you generate the final CSS independently of the web server and statically link to it. This is the highest performance approach, but requires the most manual work.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • They both seem pretty nice solutions, I will definitely check them. Do you think any of them is superior in terms of integrating with simple PHP projects ? – Spyros Sep 07 '12 at 00:49
  • I only have experience with LESS in c#; my experience base been very positive in that regard. I just added a link to a PHP port of LESS. – Tim M. Sep 07 '12 at 00:52
  • LessPHP seems pretty interesting, I will take a closer look now thanx ! http://leafo.net/lessphp/ – Spyros Sep 07 '12 at 00:52
  • If you search this site for LESS or SASS you should find a variety of info. Both are very popular. – Tim M. Sep 07 '12 at 00:53
  • Just to clarify, I +1 your answer. In my case, i will need to create sort of a panel that gets user defined values, produces a new CSS file and includes it in the page. I'm not particularly sure if LESS/SASS are a bit overkill for that, but I will definitely investigate. – Spyros Sep 07 '12 at 01:01
  • Thanks for the +1. You can definitely generate plain CSS files using just string building for simple stuff, but you could also probably take values from a user and pass those as variables to a LESS or SASS compilation process and produce a new stylesheet. – Tim M. Sep 07 '12 at 01:03
  • Even if they aren't the right solution, you can probably pull some ideas from the source code. – Tim M. Sep 07 '12 at 01:04