0

I am using the yii2 framework and the following code in appasset.php:

public $basePath = '@webroot'; 
public $baseUrl = '@web'; 
public $css = [ 'css/site.css', 'css/country.css', 'css/admin/one.css', 'css/fg/two.css' ]; 
public $js = [ ]; 
public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ];

My page pulls in all of the CSS files listed, but I only want one.css in this page, and to use the other CSS files in other pages.

How can I prevent these other CSS files from appearing in this page? Or - is it possible to create a new appasset.php file for a single php page?

1
  • Clarifying question & title, and improving readability. Commented May 9, 2015 at 18:02

1 Answer 1

1

Each specific situation should have its own AssetBundle. You just create as many as you need and include them in the relevant view file:

Existing one:

class AppAssets extends \yii\web\AssetBundle 
{ 
   public $basePath = '@webroot'; 
   public $baseUrl = '@web';
   public $css = ['css/site.css', 'css/country.css', 'css/fg/two.css'];
   ...
}  

Additional one:

class AdminAssetBundle extends \yii\web\AssetBundle 
{ 
   public $basePath = '@webroot'; 
   public $baseUrl = '@web';
   public $css = ['css/admin/one.css'];
   public $depends = ['AppAssets'],
}  

(Please add namespaces where necessary, I left those out)

As said: Then just include the one you need in the view that is relevant. ie. in your admin views you add: AdminAsset::register($this);

Because of the depends, those views will automatically include your AppAssets bundle.

If your AppAssets is the one from the yii app distribution (added by default), it is probably already being registered in the /views/layouts/main.php-file.

That means that it is not required to be defined as a dependency. I do consider it a good practice to keep the dependencies clear (if your admin one.css file actually depends on the ones from AppAssets, if not remove the $depends alltogether).

Yii is smart enough to only include each asset bundle only once anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.