0

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 ?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answer. You are right. I must investigate deeper. What I could make out is that I was using the Nav widget without putting it between Navbar:begin and Navbar:end. Since I reintroduced this Navbar things work fine and boostrap.js is reintroduced into the page.
But only registering BootstrapPluginAsset works fine as well
0

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
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.