PHPackages                             websourcecz/less.php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. websourcecz/less.php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

websourcecz/less.php
====================

PHP port of the Javascript version of LESS http://lesscss.org (Originally maintained by Josh Schmidt)

v1.7.0.15(8y ago)047Apache-2.0JavaScriptPHP &gt;=5.3

Since Apr 24Pushed 8y ago1 watchersCompare

[ Source](https://github.com/WebsourceCz/less.php)[ Packagist](https://packagist.org/packages/websourcecz/less.php)[ Docs](http://lessphp.gpeasy.com)[ RSS](/packages/websourcecz-lessphp/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (1)Dependencies (1)Versions (39)Used By (0)

[Less.php](http://lessphp.typesettercms.com)
============================================

[](#lessphp)

This is a PHP port of the official LESS processor . [![Build Status](https://camo.githubusercontent.com/e647468cc29aa608e34a186ce07ee48cff9e26ebf289025270f53677efc51020/68747470733a2f2f7472617669732d63692e6f72672f6f79656a6f7267652f6c6573732e7068702e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/oyejorge/less.php)

- [About](#about)
- [Installation](#installation)
- [Basic Use](#basic-use)
- [Caching](#caching)
- [Source Maps](#source-maps)
- [Command Line](#command-line)
- [Integration with other projects](#integration-with-other-projects)
- [Transitioning from Leafo/lessphp](#transitioning-from-leafolessphp)
- [Credits](#credits)

About
-----

[](#about)

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.

Installation
------------

[](#installation)

You can install the library with composer or manually.

#### Composer

[](#composer)

Step 1. Edit your `composer.json`:

```
{
    "require": {
        "oyejorge/less.php": "~1.7.0.9"
    }
}
```

Step 2. Install it:

```
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
```

#### Manually From Release

[](#manually-from-release)

Step 1. [Download the latest release](https://github.com/oyejorge/less.php/releases) and upload the php files to your server.

Step 2. Include the library:

```
require_once '[path to less.php]/Less.php';
```

#### Manually From Source

[](#manually-from-source)

Step 1. [Download the source](https://github.com/oyejorge/less.php/archive/master.zip) and upload the files in /lib/Less to a folder on your server.

Step 2. Include the library and register the Autoloader

```
require_once '[path to less.php]/Autoloader.php';
Less_Autoloader::register();
```

Basic Use
---------

[](#basic-use)

#### Parsing Strings

[](#parsing-strings)

```
$parser = new Less_Parser();
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
```

#### Parsing Less Files

[](#parsing-less-files)

The parseFile() function takes two arguments:

1. The absolute path of the .less file to be parsed
2. 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', 'http://example.com/mysite/' );
$css = $parser->getCss();
```

#### Handling Invalid Less

[](#handling-invalid-less)

An exception will be thrown if the compiler encounters invalid less

```
try{
	$parser = new Less_Parser();
	$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
	$css = $parser->getCss();
}catch(Exception $e){
	$error_message = $e->getMessage();
}
```

#### Parsing Multiple Sources

[](#parsing-multiple-sources)

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();
```

#### Getting Info About The Parsed Files

[](#getting-info-about-the-parsed-files)

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();
```

#### Compressing Output

[](#compressing-output)

You can tell less.php to remove comments and whitespace to generate minimized css files.

```
$options = array( 'compress'=>true );
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```

#### Getting Variables

[](#getting-variables)

You can use the getVariables() method to get an all variables defined and their value in a php associative array. Note than less have to be previously compiled

```
$parser = new Less_Parser;
$parser->parseFile( '/var/www/mysite/bootstrap.less');
$css = $parser->getCss();
$variables = $parser->getVariables();
```

#### Setting Variables

[](#setting-variables)

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( array('font-size-base'=>'16px') );
$css = $parser->getCss();
```

#### Import Directories

[](#import-directories)

By default, less.php 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 less.php where to look.

```
$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();
```

Caching
-------

[](#caching)

Compiling less code into css is a time consuming process, caching your results is highly recommended.

#### Caching CSS

[](#caching-css)

Use the Less\_Cache class to save and reuse the results of compiled less files. This 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. Note: When changes are found, this method will return a different file name for the new cached content.

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

#### Caching CSS With Variables

[](#caching-css-with-variables)

Passing options to Less\_Cache::Get()

```
$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
$options = array( 'cache_dir' => '/var/www/writable_folder' );
$variables = array( 'width' => '100px' );
$css_file_name = Less_Cache::Get( $less_files, $options, $variables );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
```

#### Parser Caching

[](#parser-caching)

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 = array('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 PHPs `var_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 PHPs `call_user_func()` and `is_callable()` functions). less.php will pass the parser object (class `Less_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. The `get` callback must return the ruleset (an array with `Less_Tree` objects) provided as fourth parameter of the `set` callback. If something goes wrong, return `NULL` (cache doesn't exist) or `FALSE`.

Source Maps
-----------

[](#source-maps)

Less.php supports v3 sourcemaps

#### Inline

[](#inline)

The sourcemap will be appended to the generated css file.

```
$options = array( 'sourceMap' => true );
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```

#### Saving to Map File

[](#saving-to-map-file)

```
$options = array(
	'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();
```

Command line
------------

[](#command-line)

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`

Integration with other projects
-------------------------------

[](#integration-with-other-projects)

#### Drupal 7

[](#drupal-7)

This library can be used as drop-in replacement of lessphp to work with [Drupal 7 less module](https://drupal.org/project/less).

How to install:

1. [Download the less.php source code](https://github.com/oyejorge/less.php/archive/master.zip) and unzip it so that 'lessc.inc.php' is located at 'sites/all/libraries/lessphp/lessc.inc.php'.
2. Download and install [Drupal 7 less module](https://drupal.org/project/less) as usual.
3. That's it :)

#### JBST WordPress theme

[](#jbst-wordpress-theme)

JBST has a built-in LESS compiler based on lessphp. Customize your WordPress theme with LESS.

How to use / install:

1. [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.
2. Find the compiler under Appearance &gt; LESS Compiler in your WordPress dashboard
3. Enter your LESS code in the text area and press (re)compile

Use the built-in compiler to:

- set any [Bootstrap](http://getbootstrap.com/customize/) variable or use Bootstrap's mixins: -`@navbar-default-color: blue;`- create a custom button: `.btn-custom { .button-variant(white; red; blue); }`
- 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 download as [plugin](http://wordpress.org/plugins/wp-less-to-css/)

#### WordPress

[](#wordpress)

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](http://tgmpluginactivation.com/)

How to install:

1. Install the plugin from your WordPress Dashboard:
2. That's it :)

Transitioning from Leafo/lessphp
--------------------------------

[](#transitioning-from-leafolessphp)

Projects 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/oyejorge/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'.

Note, the 'setPreserveComments' will no longer have any effect on the compiled less.

Credits
-------

[](#credits)

less.php was originally ported to php by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 86.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~56 days

Recently: every ~119 days

Total

38

Last Release

3061d ago

PHP version history (3 changes)v1.3.0PHP &gt;=5.3.5

1.4.2b2PHP &gt;=5.2

v1.7.0.5PHP &gt;=5.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/3385a33e0e02115a7af6773971874cc75c8bd49b30b55301377f513871f8e012?d=identicon)[websourcecz](/maintainers/websourcecz)

---

Top Contributors

[![oyejorge](https://avatars.githubusercontent.com/u/1043524?v=4)](https://github.com/oyejorge "oyejorge (1169 commits)")[![paladox](https://avatars.githubusercontent.com/u/5727000?v=4)](https://github.com/paladox "paladox (35 commits)")[![agar](https://avatars.githubusercontent.com/u/245425?v=4)](https://github.com/agar "agar (28 commits)")[![Asenar](https://avatars.githubusercontent.com/u/1180301?v=4)](https://github.com/Asenar "Asenar (17 commits)")[![Mordred](https://avatars.githubusercontent.com/u/29232?v=4)](https://github.com/Mordred "Mordred (14 commits)")[![Nodge](https://avatars.githubusercontent.com/u/1094801?v=4)](https://github.com/Nodge "Nodge (8 commits)")[![dkrnl](https://avatars.githubusercontent.com/u/734622?v=4)](https://github.com/dkrnl "dkrnl (6 commits)")[![WebsourceCz](https://avatars.githubusercontent.com/u/676930?v=4)](https://github.com/WebsourceCz "WebsourceCz (5 commits)")[![adrianbadarau](https://avatars.githubusercontent.com/u/6748623?v=4)](https://github.com/adrianbadarau "adrianbadarau (4 commits)")[![mlocati](https://avatars.githubusercontent.com/u/928116?v=4)](https://github.com/mlocati "mlocati (4 commits)")[![pafnuty](https://avatars.githubusercontent.com/u/1635679?v=4)](https://github.com/pafnuty "pafnuty (4 commits)")[![PhrozenByte](https://avatars.githubusercontent.com/u/920356?v=4)](https://github.com/PhrozenByte "PhrozenByte (4 commits)")[![ravisorg](https://avatars.githubusercontent.com/u/1596725?v=4)](https://github.com/ravisorg "ravisorg (4 commits)")[![rummik](https://avatars.githubusercontent.com/u/630909?v=4)](https://github.com/rummik "rummik (4 commits)")[![zghosts](https://avatars.githubusercontent.com/u/4441963?v=4)](https://github.com/zghosts "zghosts (4 commits)")[![nyordanov](https://avatars.githubusercontent.com/u/192220?v=4)](https://github.com/nyordanov "nyordanov (3 commits)")[![vburlak](https://avatars.githubusercontent.com/u/1781636?v=4)](https://github.com/vburlak "vburlak (3 commits)")[![reedy](https://avatars.githubusercontent.com/u/67615?v=4)](https://github.com/reedy "reedy (3 commits)")[![lduer](https://avatars.githubusercontent.com/u/3307025?v=4)](https://github.com/lduer "lduer (3 commits)")[![fabrizim](https://avatars.githubusercontent.com/u/79165?v=4)](https://github.com/fabrizim "fabrizim (3 commits)")

---

Tags

phpcsslessstylesheetless.jslesscss

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/websourcecz-lessphp/health.svg)

```
[![Health](https://phpackages.com/badges/websourcecz-lessphp/health.svg)](https://phpackages.com/packages/websourcecz-lessphp)
```

###  Alternatives

[wikimedia/less.php

PHP port of the LESS processor

12327.4M77](/packages/wikimedia-lessphp)[scssphp/scssphp

scssphp is a compiler for SCSS written in PHP.

62827.7M220](/packages/scssphp-scssphp)[mishal/iless

Less.js port to PHP

4737.0k3](/packages/mishal-iless)[dkcwd/dkcwd-zf2-munee

Zend Framework 2 module leveraging 'munee' an asset optimisation library developed by Cody Lundquist. You can find munee at http://github.com/meenie/munee

102.1k](/packages/dkcwd-dkcwd-zf2-munee)[tailwindphp/tailwindphp

A full port of TailwindCSS 4.x to PHP

171.7k1](/packages/tailwindphp-tailwindphp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
