Yii 2.0 Loading css and js in renderPartial

How can I load css and javascript files if I create the page using renderPartial ()? The problem is I need my mobile mobility. This doesn't seem to be a problem when I use render ()

I tried including boostpage css and js on my page but it still didn't work.

I have included css and js in my page:

<link rel="stylesheet" href="<?= Yii::getAlias('@web/css/bootstrap.min.css')?> ">
<link rel="stylesheet" href="<?= Yii::getAlias('@web/css/sb-admin.css')?>">
<link rel="stylesheet" href="<?= Yii::getAlias('@web/css/site.css')?>">
<link rel="stylesheet" href="<?= Yii::getAlias('@web/css/jquery-ui.css')?>">

      

AND

<script type="text/javascript" src="<?= Yii::getAlias('@web/js/jquery.js')?>"></script>
<script type="text/javascript" src="<?= Yii::getAlias('@web/js/bootstrap.min.js')?>"></script>

      

And in my controller:

return $this->renderPartial('create', [
                        'model' => $model,
            ]);

      

What am I missing?

+3


source to share


5 answers


You are looking for \yii\web\view::renderAjax()

:) This includes javascript and css, but does not include layout (and hence acts like a partial render).



+8


source


renderPartial()

don't use layout. If you need css and js, you can create a layout that suits you in the \ layouts directories and call it in your action by typing



  $ this-> layout = 'yourLayout';
  return $ this-> render ('yourForm', [
         'model' => $ yourModel,

  ]); 

      

+4


source


use renderAjax () instead of renderPartial () ... it works

different between renderPartial () and renderAjax () - renderAjax () also load js and css (asset bundle and its dependency) without layout, and renderPartial () does not load css and js as well as layout .. :)

you can use

return $this->renderAjax('_document', [
            'model' => $model,//$this->findModel($id),
        ]);

      

+3


source


If you are using renderAjax, and if you know that you do not need to load jQuery and / or JUI into a view file, just end with:

unset($this->assetBundles['yii\web\YiiAsset']);
unset($this->assetBundles['yii\web\JqueryAsset']);

      

so the renderAjax method only loads the remaining assets.

0


source


Use renderJsFile and renderCssFile

$this->registerJsFile(
    '@web/js/main.js',
    ['depends' => [\yii\web\JqueryAsset::className()]]
);

$this->registerCssFile("@web/css/themes/black-and-white.css", [
    'depends' => [\yii\bootstrap\BootstrapAsset::className()],
    'media' => 'print',
], 'css-print-theme');

      

A simple change depends on what you need. Here is the complete documentation link

0


source







All Articles