8

Well, I declared all my CSS and JavaScript stuff in AppAsset but I can't make it to display the css and js links in the in the front-end view. Here are my files:

app/assets/AppAsset.php:

<?php

namespace app\assets;

use yii\web\AssetBundle;
class AppAsset extends AssetBundle {
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'assets/css/bootstrap.min.css',
        'assets/plugins/weather-icon/css/weather-icons.min.css',
        ...
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

and here is my layout (app/modules/admin/layouts/admin.php):

<?php

use app\assets\AppAsset;

AppAsset::register($this);
?>

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php $this->head() ?>
        <?= $this->render('partials/head') ?>
    </head>
<body class="tooltips">
    <?= $this->render('partials/colour_panel') ?>
        <div class="wrapper">
            <?= $this->render('partials/top_navbar') ?>
            <?= $this->render('partials/left_sidebar') ?>
            <?= $this->render('partials/right_sidebar') ?>
            <div class="page-content">
                <div class="container-fluid">
                    <?= $content ?>
                    <?= $this->render('partials/footer') ?>
                </div>
            </div>
        </div>
        <?= $this->render('partials/scripts') ?>
    </body>
</html>
<?php $this->endPage() ?>

Thanks in advance!

4 Answers 4

10

I had same issue. In my case layout has no <?php $this->beginBody() ?> and
<?php $this->endBody() ?> sections inside <body></body> tags.

It should be like this:

<body>
<?php $this->beginBody() ?>
   <!--content-->
<?php $this->endBody() ?>
</body>

Hope it helps somebody

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

3 Comments

OK so this worked but why? these statements are not in my 'main' layout (where the JS works) but when I use a 'blank' (custom) layout the JS doesn't load without beginBody() and endBody() statements?
@gvanto Ussualy JS tags are placed before closing </body> tag (or inside <head></head>). I think $this->endBody() statements tells Yii where the end of the HTML body is. Without it Yii doesn't know where <body> ends and cannot plase <script> tags before it.
The same behavior arises if <?php $this->endPage() ?> is missing at the end
7

This part

<?php $this->head() ?>

should copy them in the head automatically. Are you sure the paths that you use are correct?

public $sourcePath = '@vendor';
public $css = [
    'assets/css/bootstrap.min.css',

means that your files are found in the vendor/assets/css/bootstrap.min.css are you sure that is right? as that looks wrong to me.

5 Comments

Yes, I am pretty sure the path to the files is okay. At present, the directory which consists all my css and js files is vendor/scripts. All the files from vendor are copied in "web/assets/2b2e6bcf" but unfortunately it seems that none of them appears in the head section of the layout. Any ideas?
Don't be pretty sure, be sure. in your project navigate to the main folder of your application then go to vendor/assets/css/bootstrap.min.css and confirm that there is a file there. No decent yii2 instalation should have anything there.
I have already checked numerous times. The files were there but were not declared in the head of the html layout. Now I decided to put them in the "web/assets" directory and again it doesn't seem to work. Check the main post.
try removing all the ->render( and use ->renderPartial( or to make a test just remove every partial views just to make sure everything is ok in the views.
I removed them all, also tried <?= $this->head() ?> instead of <?php $this->head() ?> but nothing happens.
0

Probably your asset folder file size bigger than 20MB. I think asset manager not created big folder.

Comments

0

I had the same issue because I mistakenly removed below line from frontend/views/layouts/main.php

<?php $this->head() ?> 

CSS starts working when above line added.

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.