如何在yii2中添加条件取决于AppAsset类?

时间:2015-04-27 09:48:34

标签: php twitter-bootstrap yii yii2

在Yii2中有一些依赖于AppAsset类:

public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset'
];

有没有办法为此添加条件取决于我使用css和js?

public $jsOptions = ['condition' => 'lt IE 7']; 

或者,或许,你知道另一种为bootstrap和yii JS和CSS文件添加条件的方法吗?
感谢

UPD:我已添加到config / web.php:

'components' => [
'assetManager' => [
    'bundles' => [
        'yii\web\YiiAsset' => [
            'jsOptions' => ['condition' => 'lt IE 7'],
        ],
        'yii\bootstrap\BootstrapAsset' => [
            'jsOptions' => ['condition' => 'lt IE 7'],
        ],
    ],
],

但是,我已经得到了这个(正如@arogachev推荐的那样):

<script src="/assets/8fd244c6/jquery.js"></script>
<!--[if lt IE 7]>
<script src="/assets/bd48c465/yii.js"></script>
<![endif]-->
<script src="/assets/bd48c465/yii.gridView.js"></script>
<script src="/assets/db9cb9aa/js/bootstrap.js"></script>

UPD2 :回答

'yii\web\YiiAsset' => [
            'cssOptions'    => ['condition' => 'gt IE 7]>'],
            'jsOptions'     => ['condition' => 'gt IE 7]>'],
        ],
        'yii\bootstrap\BootstrapAsset' => [
            'cssOptions'    => ['condition' => 'gt IE 7]>'],
        ],
        'yii\bootstrap\BootstrapPluginAsset' => [
            'jsOptions'     => ['condition' => 'gt IE 7]>'],
        ],
        'yii\web\JqueryAsset' => [
            'jsOptions'     => ['condition' => 'gt IE 7]>'],
            'cssOptions'    => ['condition' => 'gt IE 7]>'],
        ],

1 个答案:

答案 0 :(得分:4)

您可以通过以下应用程序配置自定义供应商包:

return [
    // ...
    'components' => [
        'assetManager' => [
            'bundles' => [
                'yii\web\YiiAsset' => [
                    'jsOptions' => ['condition' => 'lt IE 7'],
                ],
                'yii\bootstrap\BootstrapAsset' => [
                    'jsOptions' => ['condition' => 'lt IE 7'],
                ],
            ],
        ],
    ],
];

或在运行时通过assetManager

use Yii;

...

Yii::$app->assetManager->bundles['yii\web\YiiAsset']->jsOptions = ['condition' => 'lt IE 7'];

Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset']->jsOptions = ['condition' => 'lt IE 7'];

官方文档: