Prevent loading a specific CSS file in Yii

I am coding in Yii.

I registered the main.css file in the main.php layout file as follows:

Yii::app()->clientScript->registerCssFile($this->assetsBase.'/css/main.css'); 

Now this script is registered on all pages. However, let's say I do not want the script to register on a specific page.

I know that using:

 Yii::app()->clientScript->reset(); 

will delete all CSS files, however I want only a few CSS files to be unregistered.

Is there any way to do this?

+7
css php yii
source share
2 answers

You can use the scriptMap CClientScript property in your specific view by passing false for a particular script / css file that you do not want to be rendered. From the doc:

The keys to the array are the script file names (without a part of the directory), and the values โ€‹โ€‹of the array are the corresponding URLs. If the value of the array is false, the corresponding script file will not be displayed.

For example, in say views / site / pages / about.php:

 Yii::app()->clientScript->scriptMap = array( 'main.css' => false ); 

disable the inclusion of main.css registered with registerCssFile .

scriptMap can also be used to control the display of js script files.

+9
source share

I would suggest either:

Using a separate layout for this page, for example main_no-css.php
or
Adding a condition before registering the script (for example, if the controller! = Xxx and action! = Yyy), but this will lead to the condition being checked on every other page.

I would choose a separate layout.

+1
source share

All Articles