So, you wanna use LESS CSS in your PHP projects huh?

The problem with CSS pre-processing frameworks is that they don’t really fit within the average web designers’ work flow. Or they don’t mine, anyway.
Having to compile and recompile at every iteration is just a pain in the backside, it’s not the way I work. I like to make some tweaks, view them in the browser, make more tweaks, view them in the browser, and repeat.
When you add in to that the process of going to the command line and recompiling, all of a sudden writing CSS becomes a dull slog. Not the way I like to work.
The other problem is that pre-processing frameworks are normally deeply entrenched in a particular technology, such as SASS and LESS being Ruby tools.
This is fine if you’re developing CSS within your web application, but that’s not the way I work. I like to build all my HTML and CSS assets way before I go anywhere near firing up the web app - I build simple static assets that I use later.
There are a few tools out there to help this problem with regards to SASS. My favourite is StaticMatic and there is also the popular Compass. However, my new CSS framework of choice, LESS, has fewer tools available to assist.
Introducing the LESS PHP Cacher
Yesterday afternoon I wanted to demo some LESS-based work to a client. It needed some basic dynamic functionality, but I didn’t want to faff around with setting up a Ruby project. PHP would do the trick so I needed LESS to work within PHP.
So, I spent a few hours bashing together the LESS PHP Cacher.
What is DOESN'T do
OK, lets be clear, this is NOT a PHP port of LESS. I’ll leave that joyous-sounding task to someone with far more PHP wizardry skills (and time on their hands) than me.
Therefore, you still need Ruby in order to install the LESS and LESS PHP Cacher gems.
What it DOES do
First of all it installs a command line tool on your system. Using the command line tool installs a simple PHP wrapper script in to your web project.
Now you just write all your stylesheets using LESS. When you view the site in a browser, the PHP wrapper script invokes the LESS engine to parse the LESS into CSS and it creates a cached version.
That’s it.
In truth it doesn’t do a hell of a lot more than just leaving the normal LESS command line tool running in watch mode. The only difference is that you don’t have to worry about starting up daemons, you can work on multiple LESS files at once, and it might be a little bit more friendly to non-Rubyists.
The other plus point is that you can, in theory, put this on your production environment - providing you have access to the command line and can install Ruby Gems.
Whether or not you’d actually want to do that I’m not sure. I built this primarily as a development tool for my own specific needs.
How to install it
As with all Ruby Gems, installation is easy:
sudo gem install less_php_cacher
It requires LESS (obviously) so make sure that’s installed too.
How to prepare a project
Hop into your command prompt and go to the root of your existing web project. Then simply type:
lessphpc prepare
That creates the PHP wrapper script (called less.php) and a cache directory.
How to integrate LESS into my project
Write your LESS-based stylesheets and save them where-ever you like within your web project. Then reference them in your source code by either:
<link rel="stylesheet" href="less.php?style.less" type="text/css" />
..or..
<?php include 'less.php'; ?> <style type="text/css"> <?php less_php('style.less'); ?> </style>
Disclaimer
Usual stuff - this is the result of only a few hours bodging about, and not at all tested other than on my own Mac. Mileage may vary.
That said, feel free to let me know how it goes - any problems you encounter or any new features you’re like to see included.
1 response
Michael responded on with…
Hi
I had the same problem.. but found another solution => http://www.below.ch/lesso
cheers, Michael
Comments are closed
Responses for this article have now been disabled. You can still email me directly through the contact form.