YII 如何在当前页面外部加载css,img,js文件

来源:互联网 发布:ssd硬盘检测软件 编辑:程序博客网 时间:2024/06/05 16:53

from http://hi.baidu.com/321v_cn/item/2c93f112c1fb6fa1ffded51b


第一步:

在根目錄下yiicode /themes 目錄建立一個自己的主題目錄名字,如web (web/css ,web/js,web/image樣式,JS文件,圖片)

  把自己需要的js,css,image存放這

第二步:

在protected/config/main.php 添加

 return array(

    'theme'=>'web',

);

第三步:

用GII建立自己的modules/web

  跳到modules\web\views\layouts

新建二個文件:

tools_main.php

----------------------

<!doctype html>

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="language" content="en" />

    <!-- blueprint CSS framework -->
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl."/tools/css/global.css";?>"/>

    <title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
    <?php echo $content; ?>
</body>
</html>

---------------------------------------------------

tools_column1.php

--------

<?php $this->beginContent('/layouts/tools_main'); ?>
<?php echo $content; ?>
<?php $this->endContent(); ?>

---------

第四步:

跳到:modules\web\controllers\ToolsController.php

添加如下代碼:

class ToolsController extends Controller
{
    public $layout='/layouts/tools_column1'; //引用layout配置的樣式和js及圖片文件
    public $pageTitle = 'ARC Tools';  //設置標題文件
    public function actionIndex()
    {
        $this->render('index');
    }

web/view/tools/index.php

<!-- 只需写入body内容即可,body以外的写在tools_main.php文件里 -->

--------------------------------------------------------------------------------

還有一種方法,去掉第二步,方法如下:

必需在第三步的tools_main.php引用文件時,引用方法如下:

將   

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl."/tools/css/global.css";?>"/>

改成

 <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl."/themes/web/tools/css/global.css";?>"/> 

不同就是紅色處

 

0 0
原创粉丝点击