There are two LessCSS compilers in PHP that I am aware of:
Both claim to be compatible with Bootstrap (version 3). However I am struggling to get anything going given the steep learning curve of these three facets. In short, I just want to achieve the following:
Compile multiple .less files together in PHP:
- bootstrap.less
- my.less
Compiling a single file in leafo.net/lessphp
was easy, I found:
require_once 'lessc.inc.php';
$less = new lessc;
$less->checkedCompile("my.less", "mainless.css");
However I am not sure if the library is designed for multiple files. (If so, how could/should it be done?)
I decided to move on to a different library which explicitly demonstrated how to compile for bootstrap: lessphp.gpeasy.com
. However their example snippet has left me scratching my head (taken from here):
<?php
require 'less.php/LessCache.php';
$files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
Less_Cache::$cache_dir = '/var/www/writable_folder';
$css_file_name = Less_Cache::Get( $to_cache );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
After looking at their other examples, I realised that $files
and $to_cache
were a typo: they are meant to be the same variable. But after reading documentation and looking at source I gave up trying to work out if the following strings were accurately conveying their purpose:
/var/www/mysite/bootstrap.less
- Is this the less file to be compiled?/mysite/
- what is this for??/var/www/writable_folder
- Is this where the css is written to?
Please could someone give me a snippet that would compile bootstrap.less
and my.less
into css file(s) using PHP?
AFAIK http://leafo.net/lessphp/ is not compatible with Boostrap 3 > 3.0.0, see https://github.com/leafo/lessphp/issues/503
http://lessphp.gpeasy.com/ works for me i have successful used it for JBST (https://github.com/bassjobsen/jamedo-bootstrap-start-theme/issues/82) i didn't use the cache function.
Without caching you can call
$parser->parseFile()
for every file you include, it will compile to one file.array(/var/www/mysite/bootstrap.less' => '/mysite/');
. Use an array of array's for multiple files:array(array(/var/www/mysite/bootstrap.less' => '/mysite/'), array(/var/www/mysite/.less' => '/mysite/'));
Less_Cache::Get seems to work for only one file a time.update As commented by @user697576
Less_Cache::Get()
accepts a single file or a list (array of files). A single file will be array likearray(/var/www/mysite/bootstrap.less' => '/mysite/');
From the docs:
I highlight including imported files cause this seems to solve your problem. Merge your files with LESS:
styles.less:
And compile styles.less only.
Yes, why not? Make sure the files imported in bootstrap.less are available in the same directory as bootstrap.less, or use
$parser->SetImportDirs()
to set the right path.It setting the right path. If you LESS file contain for example
url(image.png)
it outputsurl( /mysite/image.png)
. Note Bootstrap use ../ for the path of the glyphicons, this won't be corrected or even worse become /mysite/../. You will have to set this path in LESS:@icon-font-path: "/mysite/fonts/";
Nope after
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
$compiled
contains the (compiled) CSS you have to write this to a file. Or use: $css_file_name = Less_Cache::Get( $to_cache ); in your HTML:update Notice that since Bootstrap 3.2.0. prefixing of the properties in Bootstrap is be done by the autoprefixer in the build process. The preceding means that the Bootstrap Less code code contain property declaration which only use the W3C syntax whilst prefixing is required for cross browser support. Compiling your source with less.php does not run the autoprefixer. See also: https://github.com/oyejorge/less.php/issues/158