Skip to content
Browse files

Merge pull request #1 from rolies106/master

Adding importDir option to clientScript component
  • Loading branch information...
2 parents baa2acf + dcf0746 commit 2e34e9cb0da4431fe89864184b31881ed1355aff @Andrekra committed Dec 10, 2012
Showing with 66 additions and 59 deletions.
  1. +29 −25 LessClientscript/LClientscript.php
  2. +37 −34 README.md
View
54 LessClientscript/LClientscript.php
@@ -5,47 +5,48 @@
* orginal url: https://github.com/zsoltlengyelit/LessCSS-for-Yii
* edited by André Kramer
*/
-class LClientscript extends CClientScript{
+class LClientScript extends CClientScript{
public $caching = true; //use cached less css file if available
public $compress = false; //compress less css to 1 line
+ public $importDir = array();
- public function registerCssFile($url,$media='')
+ public function registerCssFile($url, $media='')
{
//if the file extension is .less, use the lessparser.
$file_extension = end(explode('.',$url));
if($file_extension == 'less'){
$url = $this->compileLess($url, false);
}
- //run the parent method to parse the less/css file like Yii normally does.
+ //run the parent method to parse the less/css file like Yii normally does.
return parent::registerCssFile($url, $media);
}
protected function compileLess($less_input, $returnString = true)
{
- //webroot of the application
- $basepath = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR;
-
- //path to assets (default /assets)
- $assetspath = Yii::app()->getAssetManager()->basePath . DIRECTORY_SEPARATOR;
+ //webroot of the application
+ $basepath = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR;
+
+ //path to assets (default /assets)
+ $assetspath = Yii::app()->getAssetManager()->basePath . DIRECTORY_SEPARATOR;
$assetName = md5($less_input);
//reference where to store the temporary css file, before sending it to registerCSSFile
$assetCssOrig = $assetspath . $assetName . '.css';
$parsed = false;
- //only parse the file if caching is off and the file doesn't exist
- //Todo: Perhaps a check if the content is altered (date_modified) or file size changed
+ //only parse the file if caching is off and the file doesn't exist
+ //Todo: Perhaps a check if the content is altered (date_modified) or file size changed
if(!($this->caching && is_file($assetCssOrig))){
- //make the path absolute, but make sure basepath isn't in the url first.
+ //make the path absolute, but make sure basepath isn't in the url first.
$sourcepath = $basepath . str_replace($basepath, '', $less_input);
if (is_file($sourcepath))
{
- //start parsing the less file and store it in assets folder
+ //start parsing the less file and store it in assets folder
if($parsed = $this->parseLess(file_get_contents($sourcepath))){
- file_put_contents($assetCssOrig, $parsed);
- //@chmod($assetCssOrig, 0777);
- }
+ file_put_contents($assetCssOrig, $parsed);
+ //@chmod($assetCssOrig, 0777);
+ }
}
else {
throw new CException(__CLASS__.': Less stylesheet not found: '. $sourcepath);
@@ -65,20 +66,23 @@ protected function parseLess($input)
$parsed = false;
try {
- //include lessc library
+ //include lessc library
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include'. DIRECTORY_SEPARATOR . 'lessc.inc.php');
-
- //setup the parser
+
+ //setup the parser
$lessc = new lessc();
- if($this->compress){
- $lessc->setFormatter("compressed");
- }
- $lessc->importDir = dirname($input); // Set the correct context when using @import
+ if($this->compress){
+ $lessc->setFormatter("compressed");
+ }
+
+ $importDir = array_merge($this->importDir, array(dirname($input)));
+
+ $lessc->setImportDir($importDir);
- //parse the file
- $parsed = $lessc->parse($input);
+ //parse the file
+ $parsed = $lessc->parse($input);
} catch (exception $e) {
- throw new CException(__CLASS__.': Failed to compile less file with message: '.$e->getMessage().'.');
+ throw new CException(__CLASS__.': Failed to compile less file with message: '.$e->getMessage().'.');
}
return $parsed;
}
View
71 README.md
@@ -1,42 +1,45 @@
-<h1>Installation</h1>
-<p>
- Copy this repo to your Yii application extension folder (default protected/extensions)
-</p>
-
-<h1>Usage</h1>
-<p>
- Add the extension to your configuration
- <pre>
+# Installation
+
+Copy this repo to your Yii application extension folder (default protected/extensions)
+
+# Usage
+
+Add the extension to your configuration
+```php
+ ...
'components' => array(
+ ...
'clientScript' => array(
'class' => 'ext.LessClientscript.LClientscript',
'caching' => true,
- 'compress' => false
+ 'compress' => false,
+ 'importDir' => array('themes/classic/less/')
)
+ ...
)
- </pre>
- To call it into your view
- <pre>
- // SiteController.php file
- public function actionIndex()
- {
+ ...
+```
+
+To call it into your view
+
+```php
+ // SiteController.php file
+ public function actionIndex()
+ {
Yii::app()->clientScript->registerCSSFile('/assets/css/test.less','screen');
- $this->render('index');
- }
- </pre>
-
-</p>
-<h1>Attributes</h1>
-<p>
- caching: Boolean true or false, default true
- Use a cached version of the compiled less if available.
-
- compress: Boolean, default false
- Removes whitespace and linearizes the css to 1 line.
-</p>
-<h1>Credits</h1>
-<p>
- Based on LessCSS for Yii by zsoltlengyelit (https://github.com/zsoltlengyelit/LessCSS-for-Yii)
- Lessc parser by Leaf Corcoran, http://leafo.net/lessphp
-</p>
+ $this->render('index');
+ }
+```
+
+# Attributes
+
+caching: Boolean true or false, default true
+Use a cached version of the compiled less if available.
+
+compress: Boolean, default false
+Removes whitespace and linearizes the css to 1 line.
+
+# Credits
+Based on LessCSS for Yii by [zsoltlengyelit](https://github.com/zsoltlengyelit/LessCSS-for-Yii)
+Lessc parser by Leaf [Corcoran](http://leafo.net/lessphp)

0 comments on commit 2e34e9c

Please sign in to comment.
Something went wrong with that request. Please try again.