This is a PHP port of the official LESS processor.
- About
- Installation
- Security
- Basic Use
- Caching
- Source Maps
- Command Line
- Integration with other projects
- Transitioning from Leafo/lessphp
- Credits
The code structure of Less.php mirrors that of the official processor which helps us ensure compatibility and allows for easy maintenance.
Please note, there are a few unsupported LESS features:
- Evaluation of JavaScript expressions within back-ticks (for obvious reasons).
- Definition of custom functions.
You can install the library with Composer or manually.
- Install Composer
- Run
composer require wikimedia/less.php
-
Download a release and upload the PHP files to your server.
-
Include the library:
require_once '[path to less.php]/lib/Less/Autoloader.php';
Less_Autoloader::register();
The LESS processor language is powerful and including features that can read or embed arbitrary files that the web server has access to, and features that may be computationally exensive if misused.
In general you should treat LESS files as being in the same trust domain as other server-side executables, such as Node.js or PHP code. In particular, it is not recommended to allow people that use your web service to provide arbitrary LESS code for server-side processing.
See also SECURITY.
$parser = new Less_Parser();
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
The parseFile() function takes two arguments:
- The absolute path of the .less file to be parsed
- The url root to prepend to any relative image or @import urls in the .less file.
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
An exception will be thrown if the compiler encounters invalid LESS.
try{
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
}catch(Exception $e){
$error_message = $e->getMessage();
}
Less.php can parse multiple sources to generate a single CSS file.
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
Less.php can tell you which .less
files were imported and parsed.
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
$imported_files = $parser->allParsedFiles();
You can tell Less.php to remove comments and whitespace to generate minimized CSS files.
$options = [ 'compress' => true ];
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
You can use the getVariables()
method to get an all variables defined and
their value in a php associative array. Note that LESS has to be previously
compiled.
$parser = new Less_Parser;
$parser->parseFile( '/var/www/mysite/bootstrap.less');
$css = $parser->getCss();
$variables = $parser->getVariables();
You can use the ModifyVars()
method to customize your CSS if you have variables stored in PHP associative arrays.
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->ModifyVars( [ 'font-size-base' => '16px' ] );
$css = $parser->getCss();
By default, Less.php will look for imported files in the directory of the file passed to parseFile()
.
If you're using parse()
, or if import files reside in a different directory, you can tell Less.php where to look.
$directories = [ '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' ];
$parser = new Less_Parser();
$parser->SetImportDirs( $directories );
$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
$css = $parser->getCss();
Compiling LESS code into CSS is a time-consuming process, caching your results is highly recommended.
Use the Less_Cache
class to save and reuse the results of compiled LESS files.
This class will check the modified time and size of each LESS file (including imported files) and regenerate a new CSS file when changes are found.
Note: When changes are found, this method will return a different file name for the new cached content.
$less_files = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$css_file_name = Less_Cache::Get( $less_files, $options );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
Passing options to Less_Cache::Get()
:
$less_files = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$variables = [ 'width' => '100px' ];
$css_file_name = Less_Cache::Get( $less_files, $options, $variables );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
Less.php will save serialized parser data for each .less
file if a writable folder is passed to the SetCacheDir()
method.
Note: This feature only caches intermediate parsing results to improve the performance of repeated CSS generation.
Your application should cache any CSS generated by Less.php.
$options = [ 'cache_dir'=>'/var/www/writable_folder' ];
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
You can specify the caching technique used by changing the cache_method
option. Supported methods are:
php
: Creates valid PHP files which can be included without any changes (default method).var_export
: Like "php", but using PHP'svar_export()
function without any optimizations. It's recommended to use "php" instead.serialize
: Faster, but pretty memory-intense.callback
: Use custom callback functions to implement your own caching method. Give the "cache_callback_get" and "cache_callback_set" options with callables (see PHP'scall_user_func()
andis_callable()
functions). Less.php will pass the parser object (classLess_Parser
), the path to the parsed .less file ("/some/path/to/file.less") and an identifier that will change every time the .less file is modified. Theget
callback must return the ruleset (an array withLess_Tree
objects) provided as fourth parameter of theset
callback. If something goes wrong, returnNULL
(cache doesn't exist) orFALSE
.
Less.php supports v3 sourcemaps.
The sourcemap will be appended to the generated CSS file.
$options = [ 'sourceMap' => true ];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
$options = [
'sourceMap' => true,
'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map',
'sourceMapURL' => '/mysite/writable_folder/filename.map',
];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
An additional script has been included to use the compiler from the command line. In the simplest invocation, you specify an input file and the compiled CSS is written to standard out:
$ lessc input.less > output.css
By using the -w
flag you can watch a specified input file and have it compile as needed to the output file:
$ lessc -w input.less output.css
Errors from watch mode are written to standard out.
For more help, run lessc --help
This library can be used as drop-in replacement of lessphp to work with Drupal 7 less module.
How to install:
- Download the Less.php source code and unzip it so that 'lessc.inc.php' is located at 'sites/all/libraries/lessphp/lessc.inc.php'.
- Download and install Drupal 7 less module as usual.
- That's it :)
JBST has a built-in LESS compiler based on lessphp. Customize your WordPress theme with LESS.
How to use / install:
- Download the latest release copy the files to your {wordpress/}wp-content/themes folder and activate it.
- Find the compiler under Appearance > LESS Compiler in your WordPress dashboard
- Enter your LESS code in the text area and press (re)compile
Use the built-in compiler to:
- set any Bootstrap variable or use Bootstrap's mixins:
@navbar-default-color: blue;
- create a custom button:
.btn-custom { .button-variant(white; red; blue); }
- create a custom button:
- set any built-in LESS variable: for example
@footer_bg_color: black;
sets the background color of the footer to black - use built-in mixins: - add a custom font:
.include-custom-font(@family: arial,@font-path, @path: @custom-font-dir, @weight: normal, @style: normal);
The compiler can also be downloaded as plugin
This simple plugin will simply make the library available to other plugins and themes and can be used as a dependency using the TGM Library
How to install:
- Install the plugin from your WordPress Dashboard: https://wordpress.org/plugins/lessphp/
- That's it :)
Projects looking for an easy transition from leafo/lessphp can use the lessc.inc.php adapter. To use, Download the Less.php source code and unzip the files into your project so that the new lessc.inc.php
replaces the existing lessc.inc.php
.
Note, the setPreserveComments
will no longer have any effect on the compiled LESS.
Less.php was originally ported to PHP in 2011 by Matt Agar and then updated by Martin Jantošovič in 2012. From 2013 to 2017, Josh Schmidt lead development of the library. Since 2019, the library is maintained by Wikimedia.