Sankalp Singha
Sankalp Singha

Reputation: 4546

Preventing a specific CSS file from getting loaded in Yii

I am coding in Yii.

I have registered the main.css file in my main.php layout file like so :

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

Now this script is being registered in all the pages. However, say I don't want that script to be registered on a specific page.

I know that using :

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

would remove all the CSS files, however I want only few CSS files to be unregistered.

Is there a way to do it?

Upvotes: 7

Views: 5176

Answers (2)

bool.dev
bool.dev

Reputation: 17478

You can use the scriptMap property of CClientScript in your specific view, passing false for the particular script/css file's key that you don't want to be rendered. From the doc:

The array keys are script file names (without directory part) and the array values are the corresponding URLs. If an array value is false, the corresponding script file will not be rendered.

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

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

to disable the inclusion of main.css registered with registerCssFile.

The scriptMap can also be used for controlling the rendering of js script files.

Upvotes: 9

Mash
Mash

Reputation: 1339

I would suggest either:

Using a separate layout for that page like main_no-css.php
or
Adding a condition before registering the script (like if controller != xxx and action != yyy), but this would cause the condition to be checked on every other page.

I would definitely go for a separate layout.

Upvotes: 1

Related Questions