10

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:

  1. bootstrap.less
  2. 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:

  1. /var/www/mysite/bootstrap.less - Is this the less file to be compiled?
  2. /mysite/ - what is this for??
  3. /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?

Brian
  • 14,610
  • 7
  • 35
  • 43
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • 2
    I've updated the example on http://lessphp.gpeasy.com/Bootstrap to hopefully make things clearer. Thanks for finding that typo! Also, could you tag this as "less.php"? – user697576 Jan 04 '14 at 02:08
  • ffs, did they *have* to collide names like that? Infuriating. – Charles Jan 04 '14 at 10:57
  • Sorry about the name collision. I inherited the "less.php" name/project from other users on github. – user697576 Jan 04 '14 at 17:29
  • Would someone be kind enough to post a real life example of the snippet that the poster asked for? Thanks! – Ralf Aug 18 '14 at 17:44

4 Answers4

10

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/'));

<?php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
$parser->parseFile( '/var/www/mysite/mainless.css', 'http://example.com/mysite/' );
$css = $parser->getCss();

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 like array(/var/www/mysite/bootstrap.less' => '/mysite/');

From the docs:

Use the Less_Cache class to save and reuse the results of compiled less files. This method we check the modified time of each less file (including imported files) and regenerate when changes are found.

I highlight including imported files cause this seems to solve your problem. Merge your files with LESS:

styles.less:

@import "my.less"; 
@import "mainless.css";

And compile styles.less only.

/var/www/mysite/bootstrap.less - Is this the less file to be compiled?

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.

/mysite/ - what is this for??

It setting the right path. If you LESS file contain for example url(image.png) it outputs url( /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/";

/var/www/writable_folder - Is this wherethe css is written to?

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:

    <link rel="stylesheet" type="text/css" href="/writable_folder/<?php echo $css_file_name;?>"> 

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

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • 2
    "Less_Cache::Get seems to work for only one file a time." It actually does work for multiple files – user697576 Jan 03 '14 at 23:39
  • Re: `Yes, why not?` ... because of two lines in the source of Less_Cache that I found: `foreach($less_files as $file_path => $uri_or_less ){` and `$parser->ParseFile( $file_path, $uri_or_less );` (line 112 and 120). It seemed like both the key and the value were less sources, rather than a less source and css destination - confused the heck out of me – Jodes Jan 04 '14 at 11:13
  • 1
    your input should an array (single file) or an array of array's. Same as for `parseFile()`. – Bass Jobsen Jan 04 '14 at 11:24
1

another solution using http://leafo.net/lessphp:

1: get the css code for your .less files:

$baseCSS = file_get_contents("style.less");

$baseCSS .= file_get_contents("default.less");//append the second file to the variable

2: compile using:

$finalCSSCode = $less->compile($baseCSS);

cheers

Gabriel
  • 11
  • 2
  • I am working on Customizer of Wordpress. I am using like this but it's not working: `$theme_options = array( 'color_primary' => get_theme_mod( 'color_primary' ), 'color_secondary' => get_theme_mod( 'color_secondary' ), 'color_success' => get_theme_mod( 'color_success' ), 'color_alert' => get_theme_mod( 'color_alert' ) ) $theme_options_data = ' '; foreach ( $theme_options AS $key => $val ) { $theme_options_data .= "@{$key}: {$val};\n"; } $theme_options_data .= file_get_contents( $fileout ); $css = $compiler->compile( $theme_options_data );` – huykon225 Jun 22 '17 at 10:31
0

Honestly, I am still trying to understand the advantage of having the server compile the LESS files, rather than compiling them during development and then just loading the CSS onto the server when deploying the app. It just seems like extra work for the server with no advantage. I use a tool called Prepros -- the Windows equivalent of Code Kit. It's easy to set up. I make my changes in LESS and save. The CSS files are recompiled automatically and saved to the application folder on my development machine. When everything is the way I want it, I minify the files and port them to the production box.

SeanOlson
  • 379
  • 1
  • 9
  • Unless you need dynamic CSS the is NO advantage! There is nothing wrong with your work flow in my opinion on the other hand i don't think you answer the question. – Bass Jobsen Jan 03 '14 at 22:56
  • 3
    You're right, it's not the answer. Dynamic CSS, that would be the reason to put it on the server -- allowing users to skin the app or something like that I suppose. Thanks for the question, it got me thinking. – SeanOlson Jan 03 '14 at 23:52
  • 2
    For me it's just a matter of preference, I use PHP scripts on a localhost WAMP server for various compiling and so on, including for managing a subversion repository. I see it as more portable than having a set of independent workflow tools, also leveraging my familiarity with PHP. Perhaps the creators of the PHP less compilers have other reasons too. – Jodes Jan 04 '14 at 10:46
  • In fact the caching here will be server side, so the cache check will still overhead for your users. It differs from client side compiling. – Bass Jobsen Jan 04 '14 at 11:30
  • To me, it would be an advantage during development for the server to compile the code instead of the browser. Of course there's no theoretical advantage after development, raw files are better in that case. – Coded Monkey Nov 04 '14 at 20:03
0

The only maintained and currently up to date repo for a PHP based LESS compiler seems to be the one from Wikimedia.

github.com/wikimedia/less.php

Don't understand why it has so few stars. I've switched to it from leafo's lessphp by just adding the source code to the project's vendor directory and requiring vendor/lessphp/lessc.inc.php. Everything else seems to work as with leafo's library.

FullStack Alex
  • 1,783
  • 19
  • 31