• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..10-Dec-2021-

bin/H11-Dec-2020-192157

lib/Less/H11-Dec-2020-10,8636,950

CHANGES.mdH A D11-Dec-20203.1 KiB7158

LICENSEH A D11-Dec-20209.5 KiB179150

README.mdH A D11-Dec-202010.6 KiB316225

lessc.inc.phpH A D11-Dec-20207.1 KiB275191

README.md

1[![Continuous Integration](https://github.com/wikimedia/less.php/workflows/PHP%20Test/badge.svg)](https://github.com/wikimedia/less.php/actions)
2
3[Less.php](http://lessphp.typesettercms.com)
4========
5
6This is the Wikimedia fork of a PHP port of the official LESS processor <http://lesscss.org>.
7
8* [About](#about)
9* [Installation](#installation)
10* [Basic Use](#basic-use)
11* [Caching](#caching)
12* [Source Maps](#source-maps)
13* [Command Line](#command-line)
14* [Integration with other projects](#integration-with-other-projects)
15* [Transitioning from Leafo/lessphp](#transitioning-from-leafolessphp)
16* [Credits](#credits)
17
18
19
20About
21---
22The code structure of less.php mirrors that of the official processor which helps us ensure compatibility and allows for easy maintenance.
23
24Please note, there are a few unsupported LESS features:
25
26- Evaluation of JavaScript expressions within back-ticks (for obvious reasons).
27- Definition of custom functions.
28
29
30Installation
31---
32
33You can install the library with Composer or manually.
34
35#### Composer
36
371. [Install Composer](https://getcomposer.org/download/)
382. Run `composer require wikimedia/less.php`
39
40#### Manually From Release
41
42Step 1. [Download a release](https://github.com/wikimedia/less.php/releases) and upload the PHP files to your server.
43
44Step 2. Include the library:
45
46```php
47require_once '[path to less.php]/lib/Less/Autoloader.php';
48Less_Autoloader::register();
49```
50
51Basic Use
52---
53
54#### Parsing Strings
55
56```php
57$parser = new Less_Parser();
58$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
59$css = $parser->getCss();
60```
61
62
63#### Parsing LESS Files
64The parseFile() function takes two arguments:
65
661. The absolute path of the .less file to be parsed
672. The url root to prepend to any relative image or @import urls in the .less file.
68
69```php
70$parser = new Less_Parser();
71$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
72$css = $parser->getCss();
73```
74
75
76#### Handling Invalid LESS
77An exception will be thrown if the compiler encounters invalid LESS.
78
79```php
80try{
81	$parser = new Less_Parser();
82	$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
83	$css = $parser->getCss();
84}catch(Exception $e){
85	$error_message = $e->getMessage();
86}
87```
88
89
90#### Parsing Multiple Sources
91less.php can parse multiple sources to generate a single CSS file.
92
93```php
94$parser = new Less_Parser();
95$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
96$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
97$css = $parser->getCss();
98```
99
100#### Getting Info About The Parsed Files
101less.php can tell you which .less files were imported and parsed.
102
103```php
104$parser = new Less_Parser();
105$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
106$css = $parser->getCss();
107$imported_files = $parser->allParsedFiles();
108```
109
110
111#### Compressing Output
112You can tell less.php to remove comments and whitespace to generate minimized CSS files.
113
114```php
115$options = array( 'compress'=>true );
116$parser = new Less_Parser( $options );
117$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
118$css = $parser->getCss();
119```
120
121#### Getting Variables
122You can use the getVariables() method to get an all variables defined and
123their value in a php associative array. Note that LESS has to be previously
124compiled.
125```php
126$parser = new Less_Parser;
127$parser->parseFile( '/var/www/mysite/bootstrap.less');
128$css = $parser->getCss();
129$variables = $parser->getVariables();
130
131```
132
133
134
135#### Setting Variables
136You can use the ModifyVars() method to customize your CSS if you have variables stored in PHP associative arrays.
137
138```php
139$parser = new Less_Parser();
140$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
141$parser->ModifyVars( array('font-size-base'=>'16px') );
142$css = $parser->getCss();
143```
144
145
146#### Import Directories
147By default, less.php will look for @imports in the directory of the file passed to parseFile().
148If you're using parse() or if @imports reside in different directories, you can tell less.php where to look.
149
150```php
151$directories = array( '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' );
152$parser = new Less_Parser();
153$parser->SetImportDirs( $directories );
154$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
155$css = $parser->getCss();
156```
157
158
159Caching
160---
161Compiling LESS code into CSS is a time consuming process, caching your results is highly recommended.
162
163
164#### Caching CSS
165Use the Less_Cache class to save and reuse the results of compiled LESS files.
166This method will check the modified time and size of each LESS file (including imported files) and regenerate a new CSS file when changes are found.
167Note: When changes are found, this method will return a different file name for the new cached content.
168
169```php
170$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
171$options = array( 'cache_dir' => '/var/www/writable_folder' );
172$css_file_name = Less_Cache::Get( $less_files, $options );
173$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
174```
175
176#### Caching CSS With Variables
177Passing options to Less_Cache::Get()
178
179```php
180$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
181$options = array( 'cache_dir' => '/var/www/writable_folder' );
182$variables = array( 'width' => '100px' );
183$css_file_name = Less_Cache::Get( $less_files, $options, $variables );
184$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
185```
186
187
188#### Parser Caching
189less.php will save serialized parser data for each .less file if a writable folder is passed to the SetCacheDir() method.
190Note: This feature only caches intermediate parsing results to improve the performance of repeated CSS generation.
191Your application should cache any CSS generated by less.php.
192
193```php
194$options = array('cache_dir'=>'/var/www/writable_folder');
195$parser = new Less_Parser( $options );
196$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
197$css = $parser->getCss();
198```
199
200You can specify the caching technique used by changing the ```cache_method``` option. Supported methods are:
201* ```php```: Creates valid PHP files which can be included without any changes (default method).
202* ```var_export```: Like "php", but using PHP's ```var_export()``` function without any optimizations.
203  It's recommended to use "php" instead.
204* ```serialize```: Faster, but pretty memory-intense.
205* ```callback```: Use custom callback functions to implement your own caching method. Give the "cache_callback_get" and
206  "cache_callback_set" options with callables (see PHP's ```call_user_func()``` and ```is_callable()``` functions). less.php
207  will pass the parser object (class ```Less_Parser```), the path to the parsed .less file ("/some/path/to/file.less") and
208  an identifier that will change every time the .less file is modified. The ```get``` callback must return the ruleset
209  (an array with ```Less_Tree``` objects) provided as fourth parameter of the ```set``` callback. If something goes wrong,
210  return ```NULL``` (cache doesn't exist) or ```FALSE```.
211
212
213
214Source Maps
215---
216Less.php supports v3 sourcemaps
217
218#### Inline
219The sourcemap will be appended to the generated CSS file.
220
221```php
222$options = array( 'sourceMap' => true );
223$parser = new Less_Parser($options);
224$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
225$css = $parser->getCss();
226```
227
228#### Saving to Map File
229
230```php
231$options = array(
232	'sourceMap'			=> true,
233	'sourceMapWriteTo'	=> '/var/www/mysite/writable_folder/filename.map',
234	'sourceMapURL'		=> '/mysite/writable_folder/filename.map',
235	);
236$parser = new Less_Parser($options);
237$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
238$css = $parser->getCss();
239```
240
241
242Command line
243---
244An additional script has been included to use the compiler from the command line.
245In the simplest invocation, you specify an input file and the compiled CSS is written to standard out:
246
247```
248$ lessc input.less > output.css
249```
250
251By using the -w flag you can watch a specified input file and have it compile as needed to the output file:
252
253```
254$ lessc -w input.less output.css
255```
256
257Errors from watch mode are written to standard out.
258
259For more help, run `lessc --help`
260
261
262Integration with other projects
263---
264
265#### Drupal 7
266
267This library can be used as drop-in replacement of lessphp to work with [Drupal 7 less module](https://drupal.org/project/less).
268
269How to install:
270
2711. [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip it so that 'lessc.inc.php' is located at 'sites/all/libraries/lessphp/lessc.inc.php'.
2722. Download and install [Drupal 7 less module](https://drupal.org/project/less) as usual.
2733. That's it :)
274
275#### JBST WordPress theme
276
277JBST has a built-in LESS compiler based on lessphp. Customize your WordPress theme with LESS.
278
279How to use / install:
280
2811. [Download the latest release](https://github.com/bassjobsen/jamedo-bootstrap-start-theme) copy the files to your {wordpress/}wp-content/themes folder and activate it.
2822. Find the compiler under Appearance > LESS Compiler in your WordPress dashboard
2833. Enter your LESS code in the text area and press (re)compile
284
285Use the built-in compiler to:
286- set any [Bootstrap](http://getbootstrap.com/customize/) variable or use Bootstrap's mixins:
287	-`@navbar-default-color: blue;`
288        - create a custom button: `.btn-custom {
289  .button-variant(white; red; blue);
290}`
291- set any built-in LESS variable: for example `@footer_bg_color: black;` sets the background color of the footer to black
292- use built-in mixins: - add a custom font: `.include-custom-font(@family: arial,@font-path, @path: @custom-font-dir, @weight: normal, @style: normal);`
293
294The compiler can also be downloaded as [plugin](http://wordpress.org/plugins/wp-less-to-css/)
295
296#### WordPress
297
298This simple plugin will simply make the library available to other plugins and themes and can be used as a dependency using the [TGM Library](http://tgmpluginactivation.com/)
299
300How to install:
301
3021. Install the plugin from your WordPress Dashboard: http://wordpress.org/plugins/lessphp/
3032. That's it :)
304
305
306Transitioning from Leafo/lessphp
307---
308Projects looking for an easy transition from leafo/lessphp can use the lessc.inc.php adapter. To use, [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip the files into your project so that the new 'lessc.inc.php' replaces the existing 'lessc.inc.php'.
309
310Note, the 'setPreserveComments' will no longer have any effect on the compiled LESS.
311
312Credits
313---
314less.php was originally ported to PHP by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred). This Wikimedia-maintained fork was split off from [Josh Schmidt's version](https://github.com/oyejorge/less.php).
315
316