less-compiler

Yii application component which compiles LESS files on-the-fly
5 followers

This is a Yii application component which wrappes lessphp by Leaf Corcoran - LESS PHP compiler and parser

The main feature of this extention is what you can include .less files with Yii's CClientScript component the same way as .css files. Compiler automatically track all .less files for changes and recompiles output .css file on-the-fly (this feature can be disabled in production mode). Or, you can write command to compile .less files in background.

Installation

  1. Copy the LessCompiler.php file to your protected/extentions folder
  2. Download the latest version of lessphp from http://leafo.net/lessphp and put lessc.inc.php file under protected/extentions/LessCompiler folder
  3. Add a component to your protected/config/main.php like that:
// application components
    'components'=>array(
        ...
 
        'less'=>array(
            'class'=>'LessCompiler',
            'compiledPath'=>'application.assets.css', // path to store compiled css files
            'formatter'=>'lessjs', // - lessjs / compressed / classic , see http://leafo.net/lessphp/docs/#output_formatting for details
            'forceCompile'=>false, // passing in true will cause the input to always be recompiled
            'disabled'=>false, // if set to true .less files will not compile if .css file found
        ),
 
        ...
    ),

Usage

1)

Yii::app()->less->compileFile($fileInput,$fileOutput,$useCompiledPath);

Compiles less file to a css file. Returns compiled css file path and name.

$fileInput - input .less file to compile. $fileOutput - output .css file name (optional). If not set, $fileOutput will have the same name as .less file, but its extention will be .css. $useCompiledPath - optional parameter. Defaults to true. If set to false, LessCompiler's compiledPath option will not be used.

Examples:

Yii::app()->clientScript->registerCssFile(CHtml::asset(Yii::app()->less->compileFile(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less')));
Yii::app()->clientScript->registerCssFile(Yii::app()->less->compileFile(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less','css/main.css',false));

2)

Yii::app()->less->compile($css);

Compiles less string or file to a css string. Returns compiled css string.

$css - less string or file

Examples:

Yii::app()->clientScript->registerCss('CSS_block_id',Yii::app()->less->compile(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less'));
Yii::app()->clientScript->registerCss('CSS_block_id',Yii::app()->less->compile('.block { padding: 3 + 4px }'));

Resources

Newer versions of this extension here:

Be the first person to leave a comment

Please to leave your comment.

Create extension