Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing a specific CSS file from getting loaded in Yii

Tags:

css

php

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?

like image 519
Sankalp Singha Avatar asked Jul 22 '13 14:07

Sankalp Singha


2 Answers

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.

like image 147
bool.dev Avatar answered Sep 21 '22 18:09

bool.dev


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.

like image 38
Mash Avatar answered Sep 17 '22 18:09

Mash