The dynamic stylesheet language.
This is a PHP port of the official LESS processor http://lesscss.org and should produce the same results as LESS 1.4.2.
Most of the code structure remains the same, which should allow for fairly easy updates in the future.
Namespaces, anonymous functions and shorthand ternary operators - ?:
have been removed to make this package compatible with php 5.2+.
There are still a few unsupported LESS features:
- Evaluation of JavaScript expressions within back-ticks (for obvious reasons).
- Definition of custom functions - will be added to the
\Less\Environment
class.
Download the latest release and upload the php files to your server.
<?php
$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.
<?php
$parser = new \Less\Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
$css = $parser->getCss();
php.less can parse multiple sources to generate a single css file
<?php
$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();
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. Note: When changes are found, this method will return a different file name for the new cached content.
<?php
$to_cache = 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 );
php.less can tell you which .less files were imported and parsed.
<?php
$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.
<?php
$options = array( 'compress'=>true );
$parser = new \Less\Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
By default, php.less will look for @imports in the directory of the file passed to parsefile(). If you're using parse() or if @imports reside in different directories, you can tell php.less where to look.
<?php
$directories = array( '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' );
$parser = new \Less\Parser();
$parser->SetImportDirs( $directories );
$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
$css = $parser->getCss();
php.less 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 php.less.
<?php
$parser = new \Less\Parser();
$parser->SetCacheDir( '/var/www/writable_folder' );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
php.less was originally ported to php by Matt Agar and then updated by Martin Jantošovič.