Chain
Chain

Reputation: 37

Yii include CSS and javascript file

I have create a admin module in the yii and I want the module use the customized layout. so I placed the css and js file in protected/module/admin/assets,how can I include the css and js file in this folder?

and now I have another problem: I have put the file in the protected/modules/admin/css/assetsbootstrap.min.css folder,and I can not include it anyway,even I use the absolute path.I also try it in the other way like:

<link rel="stylesheet" href="<?php echo Yii::app()->baseUrl;?>/protected/modules/admin/css/assetsbootstrap.min.css" />

is this way does not work in the module?

Upvotes: 2

Views: 2440

Answers (2)

Truongnq
Truongnq

Reputation: 367

Here you are:

$baseUrl = Yii::app()->baseUrl; 
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/yourscript.js');
$cs->registerCssFile($baseUrl.'/css/yourcss.css');

OR

by editing config/main.php

// application components
  'components'=>array(
         // ...
        'clientScript'=>array(
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1/',
                    'js'=>array('jquery.min.js'),
                )
            ),
        ),
        // ...
  ),

See more: Yii include Javascript, css file

Upvotes: 1

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

Check this extension out

Yii admin module

Open AdminModule.php, there is location where used for configuration your admin module. I just post the parts what you need

public function getAssetsUrl()
    {
        if (!isset($this->assetsUrl))
        {
            $assetsPath = Yii::getPathOfAlias('admin.assets');
            $this->assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, $this->forceCopyAssets);
        }

        return $this->assetsUrl;
    }


protected function registerCoreCss()
    {
        Yii::app()->clientScript->registerCssFile($this->getAssetsUrl() . '/css/admin.css');
    }

protected function registerScript(){
    $js_arr = array('jquery.min.js', 'jqueryui.js'); //put what js file name that you need to import from admin assets folder
    foreach($js_arr as $filename){
        Yii::app()->getClientScript()->->registerScriptFile($this->getAssetsUrl().'/js/'.$filename, CClientScript::POS_END);
    }
}

And then in init()

public function init(){
    //set import ...
    // configure module ...
    // configure component  ....

    $this->registerCoreCss();
    $this->registerScript();

}

Upvotes: 2

Related Questions