Hi,
In my application, I want to do some extra js work, so I extended the AppAsset Class like this:
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
'js/global.js'
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
- With default layout
use app\assets\AppAsset;
/* @var $this \yii\web\View */
/* @var $content string */
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
the javascripts load like this when view source page:
<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>
<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>
<script src="/dev/app/web/global.js"></script>
<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>
All the bootstrap js in global.js not working, because the page register bootstrap.js after global.js
- I move the AppAsset register to bottom of content like this:
//...
</footer>
<?php
AppAsset::register($this);
$this->endBody(); ?>
then the javascripts load:
<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>
<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>
<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>
<script src="/dev/app/web/global.js"></script>
And all the functions of bootstrap js in global.js work fine.
But now I need register many AssetBundle, base on separate view. Because I don’t want to create many layout, so I remove AppAsset register in views/layouts/main.php, then register AppAsset in bottom of a view, i.e: views/site/index.php
After I did that, the script load like case 1:
<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>
<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>
<script src="/dev/app/web/global.js"></script>
<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>
What is wrong? How can I register an AssetBundle in a view? Or is there another way to use separate AssetBundle with the same layout?
Please help.