Home > PHP Framework > YII > body text

How to introduce css and js files into yii framework

王林
Release: 2020-02-17 13:51:47
Original
1812 people have browsed it

How to introduce css and js files into yii framework

1. You can import it directly on the view page

How to introduce css and js files into yii framework

2. You can directly write native code to import it. The path is the project directory/ web/css or /js

Copy after login

Recommended tutorials: yii framework

3. You can use assetBundle to manage css styles and js scripts

Resource package Definition: basic/assets/AppAsset.php


 * @since 2.0
 */class AppAsset extends AssetBundle{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'css/base.css'
    ];
    public $js = [
        'js/sliders.js'
    ];
    public $depends = [ //依赖包,没有可以不写
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',  
    ];
 
    //定义按需加载JS方法,注意加载顺序在最后  
    public static function addScript($view, $jsfile) {  
        $view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);  
    }  
      
   //定义按需加载css方法,注意加载顺序在最后  
    public static function addCss($view, $cssfile) {  
        $view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);  
    }  }
Copy after login

Write at the beginning of the view file:

Copy after login

Up to now, we can test on the browser and find that the css and js files are not introduced , please note here, we still need the last step:

We need to add some code to the view file (Note: If we use the public view file, we can add it to the public view file, if not, we can put to a separate page)

How to introduce css and js files into yii framework

#4. There is no need to define the method in the resource package manager, just introduce it directly in the view page

AppAsset::register($this);  
//css定义一样  
$this->registerCssFile('@web/css/font-awesome.min.css',['depends'=>['api\assets\AppAsset']]);  
  
 $this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset']]);  
 //$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset'],'position'=>$this::POS_HEAD]);
Copy after login

Update To learn more programming-related content, please visit the Programming Tutorial column of the PHP Chinese website!

The above is the detailed content of How to introduce css and js files into yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!