7

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?

2 Answers 2

9

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.

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

Comments

1

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.

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.