Skip to content

Instantly share code, notes, and snippets.

@antonkomarev
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonkomarev/63246451ba9464d0a829 to your computer and use it in GitHub Desktop.
Save antonkomarev/63246451ba9464d0a829 to your computer and use it in GitHub Desktop.
<?php
return [
'components' => [
'view' => [
'class' => 'common\components\View',
],
],
];
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
use yii\web\View;
class ShivAsset extends AssetBundle
{
public $sourcePath = '@bower';
public $jsOptions = [
'condition' => 'lt IE 9',
'position' => View::POS_HEAD,
];
public $js = [
'html5shiv/dist/html5shiv.js',
'respond/dest/respond.min.js',
];
}
<?php
namespace common\components;
use Yii;
use yii\helpers\Html;
use yii\web\AssetBundle;
class View extends \yii\web\View
{
public function registerJsFile($url, $depends = [], $options = [], $key = null)
{
$url = Yii::getAlias($url);
$key = $key ?: $url;
if (empty($depends)) {
$position = isset($options['position']) ? $options['position'] : self::POS_END;
unset($options['position']);
$condition = isset($options['condition']) ? $options['condition'] : null;
unset($options['condition']);
$tag = Html::jsFile($url, $options);
if ($condition) {
$tag = "<!--[if ".$condition."]>".$tag."<![endif]-->";
}
$this->jsFiles[$position][$key] = $tag;
} else {
$am = Yii::$app->getAssetManager();
if (strpos($url, '/') !== 0 && strpos($url, '://') === false) {
$url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url;
}
$am->bundles[$key] = new AssetBundle([
'js' => [$url],
'jsOptions' => $options,
'depends' => (array) $depends,
]);
$this->registerAssetBundle($key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment