3

我在 AppAsset 中有我所有的 CSS 和 JavaScript 文件,我将它们加载到布局文件中。在我的登录页面上,我不想使用布局,所以我使用 renderPartial() 方法呈现登录视图文件。这样做时,我创建了一个新的 LoginAsset 文件并使用它来注册登录视图,但我的 CSS 和 JS 仍然没有加载。我也尝试过使用 AppAsset 包注册登录视图,但它也不会加载 CSS 和 JS。有什么我做错了吗?在我的控制器上使用 renderPartial 时如何加载我的资产包?

4

2 回答 2

3

嗨@Zack,您的问题是您没有指定结束正文和结束页面位置。我解决了我的问题,将这些代码添加到我的页面底部。

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
于 2016-02-10T22:59:09.780 回答
1

正如Pavel 所说,您应该使用renderAjax(). 请参阅下面的说明。

的输出renderPartial()是由视图生成的 HTML 的和平,没有任何 CSS 和 JS:

<div class="container">
   <input type="text" id="loginform-username" class="form-control" name="LoginForm[username]">
   <input type="password" id="loginform-password" class="form-control" name="LoginForm[password]">
   <button type="submit" class="btn btn-primary btn-block" name="login-button">Log in</button>
</div>

但是renderAjax()将所有注册在视图资产中的 CSS 和 JS 添加到输出中:

<link href="/assets/f1759119/css/bootstrap.css" rel="stylesheet">
<div class="container">
   <input type="text" id="loginform-username" class="form-control" name="LoginForm[username]">
   <input type="password" id="loginform-password" class="form-control" name="LoginForm[password]">
   <button type="submit" class="btn btn-primary btn-block" name="login-button">Log in</button>
</div>
<style>
   ...
</style>
<script>
   ...
</script>
<script src="/assets/13aa7b5d/jquery.js"></script>
<script src="/assets/302a2946/yii.js"></script>
<script src="/assets/302a2946/yii.activeForm.js"></script>

PS。我认为正确的方法是为登录表单创建单独的布局并使用通常的render()方法。

于 2016-11-23T10:30:30.340 回答