Autocompiling LESS to CSS using lessphp

So, you want to use LESS with automatic serverside compiling using lessphp?

Step 1: Create the LESS stylesheet

This is a sample stylesheet, I name it style.less as you see.

Step 2: Download and install lessphp

I download the compiler, lessphp, from its GitHub repository.

git clone git://github.com/leafo/lessphp.git

Here are the sources for lessphp in my sample installation.

Step 3: Create a style.php to make CSS from LESS

Now I need some PHP-code to feed lessphp with my style.less. I put everything together in style.php.

You can try it out by just accessing it, style.php, it should present the final CSS-code.

Review the sourcecode for style.php.

Do not forget to make the directory writable for the webserver, lessphp needs to be able to write the cache-file and the resulting css-file.

Step 4: Add stylesheet to the webpage

Just add the "stylesheet", style.php, as any normal stylesheet to your webpage.

<link rel='stylesheet' type='text/css' href='style.php' />

When you are done with development, and want to make it a production environment, just change the stylesheet, change .php to .css.

<link rel='stylesheet' type='text/css' href='style.css' />

Done!

Have fun and review the sourcecode for this example to review how its working.

Or clone the whole example, including lessphp essentials, from GitHub.

Discuss this example in the forum.