Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii 2.0在renderPartial中加载css和js_Css_Twitter Bootstrap_Yii2 - Fatal编程技术网

Yii 2.0在renderPartial中加载css和js

Yii 2.0在renderPartial中加载css和js,css,twitter-bootstrap,yii2,Css,Twitter Bootstrap,Yii2,如果使用renderPartial()呈现页面,如何加载css和javascript文件? 问题是,我需要我的网页是移动响应。当我使用render()时,这似乎不是问题 我尝试在我的页面中包含boostrap css和js,但仍然不起作用 我已将css和js包含在我的页面中: <link rel="stylesheet" href="<?= Yii::getAlias('@web/css/bootstrap.min.css')?> "> <link rel="sty

如果使用renderPartial()呈现页面,如何加载css和javascript文件? 问题是,我需要我的网页是移动响应。当我使用render()时,这似乎不是问题

我尝试在我的页面中包含boostrap css和js,但仍然不起作用

我已将css和js包含在我的页面中:

<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')?>">
我缺少什么?

不使用布局。如果您需要CSS和JS,您可以在目录
views/layouts
中创建适合您的布局,并通过输入以下内容在操作中调用它:

$this->layout = 'yourLayout';
return $this->render('yourForm', [
    'model' => $yourModel,
]); 
您正在寻找:) 这确实包括javascript和css,但不包括布局(因此充当部分呈现)

使用renderAjax()而不是renderPartial()。。。它的作品

renderPartial()和renderAjax()之间的不同之处在于,renderAjax()也加载js和css(资产包及其依赖项),而不加载布局,而renderPartial()不加载css和js以及布局:)

你可以用

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

如果您使用的是renderAjax,并且您知道不需要在视图文件中加载jQuery和/或JUI,只需在末尾添加:

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

因此renderAjax方法只加载剩下的资产

使用renderJsFile和rendercsfile

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

改变取决于你需要什么。下面是完整的文档

呈现视图以响应AJAX请求。正如@scaisEdge所说,隔离布局是一个更好的解决方案。这取决于OP想要什么。据我所知,需要的不是呈现布局,而是包含CSS和JS,这正是
renderAjax()
所做的
unset($this->assetBundles['yii\web\YiiAsset']);
unset($this->assetBundles['yii\web\JqueryAsset']);
$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');