Giter Club home page Giter Club logo

less.php's Introduction

less.php

The dynamic stylesheet language.

http://lesscss.org

about

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.

use

Installation

Download the latest release and upload the php files to your server.

Parsing Strings

<?php
$parser = new \Less\Parser();
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();

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.
<?php
$parser = new \Less\Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
$css = $parser->getCss();

Parsing Multiple Sources

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

Caching CSS

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

Getting Info About The Parsed Files

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

Compressing Output

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

Import Directories

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

Parser Caching

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

credits

php.less was originally ported to php by Matt Agar and then updated by Martin Jantošovič.

less.php's People

Contributors

oyejorge avatar agar avatar mordred avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.