3

How can I load css and javascript files if I render a page using renderPartial() ? The problem is that I NEED my page to be mobile responsive. This does not seem to be a problem when I use render()

I tried including the boostrap css and js in my page, but It still did not work.

I have included the 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 ?

5 Answers 5

10

You are looking for \yii\web\view::renderAjax() :) This does include javascript and css, but does not include the layout (and thus acts as a partial render).

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

2 Comments

Renders a view in response to an AJAX request. Isolating the layout is a better solution as @scaisEdge said.
Depends on what the OP wants. As I understand it the need was there to not render the layout but to do include the CSS and JS, which is exactly what renderAjax() does
6

renderPartial() does not use the layout. If you need the CSS and JS you can create a layout suitable for you in the directory views/layouts and call it in your action by entering:

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

Comments

4

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

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

you may use

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

Comments

1

If you are using renderAjax and If you know you do not need to load jQuery and/or JUI within the view file, just put at the end:

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

so the renderAjax method loads just the assets that are left..

Comments

0

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');

just change depends to what you need. Here is full documentation link

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.