Hey guys, im new to the forum so excuse me if i do something wrong.
Well, i have just started to use Yii 2.0 and i am having quite some issues with it regarding css and javascript.
Firstly, using the AssetBundle, are there any way of positioning CSS?
My problem here is that I have my main AppAsset bundle which contains all the required css and js. Then i have another WidgetAsset which may or may not be present on a page. But when is present i need the css of the WidgetAsset to be positioned on top of the AppAsset. Are there any solution to that?
And regarding the ajax and scripts, let me provide a scenario.
Assume i am on my homepage and i click on a button which should display a toast message.
The required css and js should be loaded via ajax.
The button will perform an ajax request to a controller action ‘/display/toast’. I am using renderAjax on a view which echo the toast widget only.
And the Toast Widget register the appropriate asset on the view:
Controller Action:
public function actionToast()
{
return $this->renderPartial('ajax');
}
View:
<?php
use frontend\widgets\Toast;
echo Toast::widget([]);
?>
Widget:
class Toast extends Widget
{
public function run()
{
ToastrAsset::register($this->getView())
$this->getView()->registerJs("toastr.success('toast message', 'toast title');");
}
}
When i am on my homepage, i execute in console:
$.get('/display/toast')
i obtain the result in my network log preview
<link href="/frontend/web/assets/a567ae9a/css/bootstrap.min.css" rel="stylesheet">
<link href="/frontend/web/assets/1edea995/css/toastr.min.css" rel="stylesheet"><script src="/frontend/web/assets/a567ae9a/js/bootstrap.min.js"></script>
<script src="/frontend/web/assets/df675a49/jquery.min.js"></script>
<script src="/frontend/web/assets/1edea995/js/toastr.min.js"></script>
<script type="text/javascript">toastr.success('toast message', 'toast title');</script>
which is good. But i wanted to know how do i add those scripts in my resources.
If i append this result to my body, it will work perfectly fine, but are there any way of adding the js to the appropriate location and the css to the head easily?
Thanks