assetparser

Less and Sass asset converter for YII2
9 followers

Only for YII2 with the new Asset Manager, convert Less and Sass files to CSS whithout external tools and executable. The sass and less files are converted with PHP librairies It replace the AssetConverter who use external tools. The Less and Sass file are converted with time source files dependency.

Requirements

YII 2.0

Usage

1)Copy the source to your project in {app}/protected/extensions/asset_parser 2)Modify assetManager in your configuration file {app}/protected/config/main.php

'assetManager' => array(
            'bundles' => require(__DIR__ . '/assets.php'),
            'converter'=>array(
                'class'=>'app\extensions\assetparser\Converter',
                'force'=>false
            )
        ),

'Force'=>true : If you want convert your sass each time without time dependency

The sass files with extension .sass are converted to a .css file The less files with extension .less are converted to a .css file The scss file with extension .scss are converted to a .css file

Example of assets config file /protected/config/assets.php

<?php
 
return array(
    'app' => array(
        'basePath' => '@wwwroot',
        'baseUrl' => '@www',
        'css' => array(
            'css/bootstrap.min.css',
            'css/bootstrap-responsive.min.css',
            'css/site.css',
            'css/less_style.less',
            'css/sass_style.sass',
        ),
        'js' => array(
 
        ),
        'depends' => array(
            'yii',
        ),
    ),
);

Resources

Q & A

Where can find a installation example of this extension ?

Decompress the archive assetparser_example.zip

copy to your yii/apps/assetparser

you need to change your apps/assetparser/.htaccess

Try it : http://localhost/yii2/apps/assetparser/

You have a sass file in yii2/apps/assetparser/css/sass_style.sass and less file in yii2/apps/assetparser/css/less_style.less

After running your application yii2/apps/assetparser/css/sass_style.css and yii2/apps/assetparser/css/less_style.css are generated

It's possible to chose a another folder that {app}/protected/extensions/assetparser ?

Yes It Is ! It's possible to adapt assetparser to your spcific environement, it's little bit complex (You need version 1.0.1)

For exeample : Yii Framework : C:\www\xxx\vendor\yiisoft\yii2\framework

Web application: C:\www\xxx\web\index.php

assertparser extension: C:\www\xxx\vendor\yii-ext\assetparser

Config file : C:\www\xxx\app\config\main.php

1) Adapt the configuration file (main.php) In my example : C:\www\xxx\app\config\main.php

<?php
 
'components' => array(
 
....
 
        'assetManager' => array(
            'bundles' => require(__DIR__ . '/assets.php'),
            'converter'=>array(
                'class'=>'app\extensions\assetparser\Converter',
                'force'=>false,
                 'parsers' => array(
                    'sass' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Sass',
                        'output' => 'css', // parsed output file type
                        'options' => array(
                            'cachePath' => '@app/runtime/cache/sass-parser' // optional options
                        ),
                    ),
                    'scss' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Sass',
                        'output' => 'css', // parsed output file type
                        'options' => array() // optional options
                    ),
                    'less' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Less',
                        'output' => 'css', // parsed output file type
                        'options' => array(
                            'auto' => true, // optional options
                            'lessParserPath' => __DIR__ . '/../../vendor/yii-ext/assetparser/vendors/lessphp/lessc.inc.php'
                        )
                    )
                )
            )
        ),
    ),
 
.....

You need to update lessParserPath defintion to you specific environement 'lessParserPath' => ' DIR . '/../../vendor/yii-ext/assetparser/vendors/lessphp/lessc.inc.php'

2) You need declare some classMap to YII, because your folder filepath are not standart with YII2 namespace

There are two possibilities

2a) Modifie your index.php ( C:\www\xxx\web\index.php)

defined('YII_DEBUG') or define('YII_DEBUG', true);

<?php
require(__DIR__ . '/../vendor/yiisoft/yii2/framework/yii.php');
 
Yii::$classMap['app\extensions\assetparser\Converter'] =__DIR__ . '/../vendor/yii-ext/assetparser/Converter.php';
Yii::$classMap['app\extensions\assetparser\Parser'] =__DIR__ . '/../vendor/yii-ext/assetparser/Parser.php';
Yii::$classMap['app\extensions\assetparser\Less'] =__DIR__ . '/../vendor/yii-ext/assetparser/Less.php';
Yii::$classMap['app\extensions\assetparser\Sass'] =__DIR__ . '/../vendor/yii-ext/assetparser/Sass.php';
 
$config = require(__DIR__ . '/../app/config/main.php');
$application = new yii\web\Application($config);
$application->run();

2b) Modifie your main.php ( C:\www\xxx\app\config\main.php)

<?php
Yii::$classMap['app\extensions\assetparser\Converter'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Converter.php';
Yii::$classMap['app\extensions\assetparser\Parser'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Parser.php';
Yii::$classMap['app\extensions\assetparser\Less'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Less.php';
Yii::$classMap['app\extensions\assetparser\Sass'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Sass.php';
 
return array(
    'id' => 'hello',
    'basePath' => dirname(__DIR__),
    'components' => array(
.....

Total 6 comments

#15791 report it
Artem Frolov at 2013/12/17 08:41am
Sass and Compass extension for Yii 1.x

@zama janan
For Yii 1.x you can use this extension which allows you to use Sass and Compass:
Sass (SCSS) and Compass support for the Yii framework

#13141 report it
Michel Bobillier (Athos99) at 2013/05/07 03:28pm
Solutions for your problems

I'have archive a YII2 application example with assertparser extension. It can be helpfull if your have some problems to config your application.

http://www.yiiframework.com/extension/assetparser/files/assetparser_example.zip

#13139 report it
Coksnuss at 2013/05/07 02:01pm
Wrong Framework

@zama janan: It seems like you are trying to get this extension run with Yii 1.1.x. But as it states in the description this extension is to be used with the public preview of Yii 2.0.

#13135 report it
zama janan at 2013/05/07 09:11am
Still no luck to run it properly.

Hi PinkBrainPlan, can you guide us how did you make it run as I followed the Howto instruction but still getting this error.

Property "CAssetManager.bundles" is not defined.

I hope that some will guide me to solve this error.

#13132 report it
PinkBrainPlan at 2013/05/07 05:05am
Works like charm

After some nice support;) It works like charm! Congrats!

#13123 report it
zama janan at 2013/05/06 09:12pm
Property "CAssetManager.bundles" is not defined.

Hi, thanks for your efforts for this extension, I try to run and configure as per your instruction and I got the subject errors.

I learning YII and it might be a basic errors but I give up not to catch this error.

I hope you get your response soon.

Leave a comment

Please to leave your comment.

Create extension