meaulnes
meaulnes

Reputation: 383

Yii2 Bootstrap assets not available when using custom layout in module

Context

I have a Yii2 application in which there is a module for administration.

I would like to use a specific layout for this module thus, in the Admin.php for the module I declare

public $layout = 'main';

and place the specific layout in the wiews/layouts folder of the module

The layout for the application uses

AppAsset::register($this);

and so do the layout for the admin module.

The AppAsset.php is trivial

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';

    public $baseUrl = '@web';
    public $css = [

        'scss/myOwnStyle.scss'
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',  
    ];
}

The functionality of Bootstrap I want to use is the tabbed view.

Results

Letting things as described above, the switching between tabs in an admin page doesn't work. Removing the specif layout from admin module, everything works well.

In fact, this is due to the absence of the Bootstrap javascript in the page. Indeed

<script src="/assets/f7ad570a/js/bootstrap.js"></script>

do not appear in the source of the page when using the specific layout.

My question

How comes the bootstrap.js script is not included in the admin pages when using my specific layout ?

Upvotes: 0

Views: 879

Answers (2)

Prabowo Murti
Prabowo Murti

Reputation: 1341

I don't know the exact reason why that happens, but try to put the javascript before the <body>, should we?

public $jsOptions = [
    'position' => \yii\web\View::POS_HEAD
];

Upvotes: 0

Bizley
Bizley

Reputation: 18021

That's because yii2-bootstrap does not load JavaScript files untill it's necessary. And it's necessary when you are using yii2-bootstrap widgets.

I'm guessing you are not calling yii2-bootstrap widget to generate Bootstrap component but rather generating it on your own.

In this case do what yii2-bootstrap is doing - register yii\bootstrap\BootstrapPluginAsset as well.

Upvotes: 2

Related Questions