防止在 Yii 中加载特定的 CSS 文件

2022-01-04 00:00:00 php css yii

我在 Yii 中编码.

I am coding in Yii.

我已经在我的 main.php 布局文件中注册了 main.css 文件,如下所示:

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.

我知道使用:

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

将删除所有 CSS 文件,但我只想取消注册几个 CSS 文件.

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

有办法吗?

推荐答案

您可以使用<特定视图中 CClientScript 的 code>scriptMap 属性,为您不想传递的特定脚本/css 文件的键传递 false被渲染.来自文档:

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:

数组键是脚本文件名(不含目录部分),数组值是对应的 URL.如果数组值为false,则不会呈现相应的脚本文件.

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.

例如,在 views/site/pages/about.php 中:

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

禁止包含使用 registerCssFile 注册的 main.css.

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

scriptMap也可以用来控制js脚本文件的渲染.

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

相关文章