Boilerplate 建议在向头部添加样式时使用 @import。
<style>@import(/example.css);</style>
Yii 使用 ClientScript 模型添加
<link type="text/css" src="/example.css" />
使用 Yii::app()->clientScript 模型来注册文件。 Yii 允许您根据需要为每个控制器或每个视图注册脚本文件。因此,您的 http 请求可能很少。我建议在主布局中注册所需的脚本/css,并根据需要添加其他脚本
Yii::app()->clientScript->registerScriptFile();
Yii 基于 MVC 模型。 V 用于查看。视图文件夹包含您的模型和控制器将根据数据类型调整的 html 元素。在视图文件夹中 Yii 使用 layout 文件夹来定义布局。
$this->layout = 'main';
该行将查找:
Protected -> views -> layout -> main.php
布局文件夹应包含 main、_htmlHead、_header 和 _footer。 renderPartial 将用于渲染不同的布局部分。这就像 HTML 的 php 包含。 $this->render 或 $this->renderPartial 的第二个参数用于将数据传递给视图。例如导航数据:
$this->renderPartial('_footer', array('nav'=>array('/link/'=>'Link Name')));
在 _htmlHead 中使用 Yii::app()->clientScript 注册所需的元素。如果您想使用不同版本的 jQuery,请使用 ScriptMap 模型,不要注册 jQuery 两次。 Yii 的 coreScript、验证和分页都是基于 jQuery 的。
$cs = Yii::app()->clientScript;
$cs->registerCssFile('/css/base.css');
$cs->registerScriptFile('/js/base.js', CClientScript::POS_END);
/* Load Script at END of DOM tree: CClientScript::POS_END */
http://www.yiiframework.com/doc/api/1.1/CClientScript
过去我在 Yii 中使用 config.php 文件来设置 assetsLocaion 参数。如果我移动我的资产,它不会破坏网站。
Yii::app()->clientScript->registerScriptFile(Yii::app()->param->assetsLocation.'/js/example.js');
样板的基本布局将在 layout/main.php 中定义。查看主题文档:http://www.yiiframework.com/doc/guide/1.1/en/topics.theming
布局文件可能如下所示:
<!doctype html>
<?php $this->renderPartial('//layouts/_Htmlhead); ?>
<body>
<div id="container">
<?php $this->renderPartial('//layouts/_header); ?>
<div id="main" role="main">
<?php echo $content; ?>
</div>
<?php $this->renderPartial('//layouts/_footer); ?>
</div>
<?php $this->renderPartial('//layouts/_footerScripts); ?>
</body>
</html>