0

I am use Yii2 advanced with Twig integration. I need include js and css files to head block and use AssetBundle for this.

If I use POS_BEGIN or POS_END it's work. Js and css load to page in current position. But if I set position as POS_HEAD render ignore this and no load files to page.

Please help. What am I doing wrong?

My AssetBundle

namespace frontend\assets;
use yii\web\AssetBundle;

class YandexAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        '/css/map.css'
    ];
    public $js = [
        'js/map.js',
        'js/test.js'
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
    public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD ];
}

And connect in Twig template

{{ use('frontend/assets/YandexAsset')}}
{{ register_yandex_asset() }}
2ball
  • 86
  • 1
  • 7

1 Answers1

0

From http://www.yiiframework.com/doc-2.0/guide-structure-assets.html :

Asset dependencies are mainly specified through the yii\web\AssetBundle::$depends property. In the AppAsset example, the asset bundle depends on two other asset bundles: yii\web\YiiAsset and yii\bootstrap\BootstrapAsset, which means the CSS and JavaScript files in AppAsset will be included after those files in the two dependent bundles.

both assets 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', are included at end of rendered page and your asset cannot be placed before them because they are depended of them.

if you want to include js file in POS_HEAD you can use registerJsFile() in your view or layout file

http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail

user1852788
  • 4,278
  • 26
  • 25