Load css only in one view

How can I load a css file in a unique view?

I read about Asset Blunde but I don’t know how can I make an Asset Blunde it only loads in a specific view.

Would it be better to use registerCss?

Regards.

Hi!

Regarding the AssetBundle:

Lets say you create following files:

/app/myasset/css/myCssFile.css

/app/myasset/myAssetBundle.php

myAssetBundle.php




namespace app\myasset;


use yii\web\AssetBundle;


class MyAssetBundle extends AssetBundle

{

    public $sourcePath = '@app/myasset';

    public $baseUrl = '@web';

   

    public $css = [

        'css/myCssFile.css',

    ];

    public $js = [

    ];

    public $depends = [

    ];

}



Then just use it in your view like:




use app\myasset\MyAssetBundle; 

MyAssetBundle::register($this);



That should be all.

Regards

1 Like

I’m going to try this and I commented you.

Regards.

In the view


$this->registerCssFile('@web/csspaththere.css');

http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerCssFile()-detail

Thanks to all.

Both solutions work for me.

Regards.