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 ?
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).
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.