Include CSS,javascript file in Yii Framework

前端 未结 18 1713
[愿得一人]
[愿得一人] 2020-11-29 15:46

How to include a Javascript or CSS file in Yii Framework?

I want to create a page on my site that has a little Javascript application running, so I want to include <

相关标签:
18条回答
  • 2020-11-29 15:47

    This was also an easy way to add script and css in main.php

    <script src="<?=Yii::app()->theme->baseUrl; ?>/js/bootstrap.min.js"></script>
    <link href="<?=Yii::app()->theme->baseUrl; ?>/css/bootstrap.css" rel="stylesheet" type="text/css">
    
    0 讨论(0)
  • 2020-11-29 15:48

    Easy in your conf/main.php. This is my example with bootstrap. You can see that here

    'components'=>array(
        'clientScript' => array(
            'scriptMap' => array(
                'jquery.js'=>false,  //disable default implementation of jquery
                'jquery.min.js'=>false,  //desable any others default implementation
                'core.css'=>false, //disable
                'styles.css'=>false,  //disable
                'pager.css'=>false,   //disable
                'default.css'=>false,  //disable
            ),
            'packages'=>array(
                'jquery'=>array(                             // set the new jquery
                    'baseUrl'=>'bootstrap/',
                    'js'=>array('js/jquery-1.7.2.min.js'),
                ),
                'bootstrap'=>array(                       //set others js libraries
                    'baseUrl'=>'bootstrap/',
                    'js'=>array('js/bootstrap.min.js'),
                    'css'=>array(                        // and css
                        'css/bootstrap.min.css',
                        'css/custom.css',
                        'css/bootstrap-responsive.min.css',
                    ),
                    'depends'=>array('jquery'),         // cause load jquery before load this.
                ),
            ),
        ),
    ),
    
    0 讨论(0)
  • 2020-11-29 15:48

    If you are using Theme then you can the below Syntax

    Yii::app()->theme->baseUrl
    

    include CSS File :

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

    Include JS File

    <script src="<?php echo Yii::app()->theme->baseUrl;?>/js/jquery-2.2.3.min.js"></script>
    

    If you are not using theme

    Yii::app()->request->baseUrl
    

    Use Like this

    <link href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
    <script src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery-2.2.3.min.js"></script>
    
    0 讨论(0)
  • 2020-11-29 15:53

    You can also add scripts from controller action. Just add this line in an action method then that script will apear only in that view:

    Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/custom.js', CClientScript::POS_HEAD);
    

    where POS_HEAD tell framework to put script in head section

    0 讨论(0)
  • 2020-11-29 15:54

    here is a good solution to use CDN and offline scripts

    I use this code in every application I build, so you can use this in any app.

    Included Scripts:

    • main.css
    • main.js
    • jQuery
    • jQuery / CD
    • Bootstrap 3.1
    • Bootstrap 3.1 / CDN
    • Fancybox 2
    • Fancybox 2 / CDN
    • FontAwesome 4
    • FontAwesome 4 / CDN
    • Google Analytics Script

    STEP1:

    put this code in config/main.php

            'params'=>array(
                'cdn'=>true, // or false
    ...
    

    STEP2:

    create resoreses folder in root app folder and put your script there

    res/
    --js
    --css
    --img
    --lib
    --style
    ..
    

    STEP3:

    put this code in components/controller.php

    public function registerDefaults() 
    {
        $cs = Yii::app()->clientScript;
    
        if (Yii::app()->params['cdn']){
            $cs->scriptMap = array(
                'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js',
                'jquery.min.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js',
            );
            $cs->packages = array(
                'bootstrap' => array(
                    'basePath' => 'application.res',
                    'baseUrl' => '//netdna.bootstrapcdn.com/bootstrap/3.1.1/',
                    'js' => array('js/bootstrap.min.js'),
                    'css' => array('css/bootstrap.min.css'),
                    'depends' => array('jquery')
                ),
            );
        } else {
            $cs->packages = array(
                'bootstrap' => array(
                    'basePath' => 'application.res',
                    'baseUrl' => Yii::app()->baseUrl . '/res/lib/bootstrap/',
                    'js' => array('js/bootstrap.js'),
                    'css' => array('css/bootstrap.css'),
                    'depends' => array('jquery')
                ),
            );
        }
    
        $cs->registerPackage('bootstrap');
    
        $cs->registerCSSFile(Yii::app()->baseUrl . '/res/style/main.css');
        $cs->registerScriptFile(Yii::app()->baseUrl . '/res/js/main.js');
    }
    
    public function registerFancybox($buttons = false, $thumbs = false) 
    {
        $cs = Yii::app()->clientScript;
    
        $cs->packages = array(
            'fancybox' => array(
                'basePath' => 'application.res',
                'baseUrl' => Yii::app()->baseUrl . '/res/lib/fancybox/',
                'js' => array('lib/jquery.mousewheel-3.0.6.pack.js', 'source/jquery.fancybox.pack.js'),
                'css' => array('source/jquery.fancybox.css'),
                'depends' => array('jquery')
            ),
            'fancybox-buttons' => array(
                'basePath' => 'application.res',
                'baseUrl' => Yii::app()->baseUrl . '/res/lib/fancybox/source/helpers/',
                'js' => array('jquery.fancybox-buttons.js'),
                'css' => array('jquery.fancybox-buttons.css'),
            ),
            'fancybox-thumbs' => array(
                'basePath' => 'application.res',
                'baseUrl' => Yii::app()->baseUrl . '/res/lib/fancybox/source/helpers/',
                'js' => array('jquery.fancybox-thumbs.js'),
                'css' => array('jquery.fancybox-thumbs.css'),
            )
        );
    
        $cs->registerPackage('fancybox');
        if ($buttons)
            $cs->registerPackage('fancybox-buttons');
        if ($thumbs)
            $cs->registerPackage('fancybox-thumbs');
    }
    
    public function registerFontAwesome(){
    
        $cs = Yii::app()->clientScript;
    
        if (Yii::app()->params['cdn']):
            $cs->packages = array(
                'fontAwesome' => array(
                    'basePath' => 'application.res',
                    'baseUrl' => '//netdna.bootstrapcdn.com/font-awesome/4.0.0/',
                    'css' => array('css/font-awesome.min.css'),
                )
            );
        else:
            $cs->packages = array(
                'fontAwesome' => array(
                    'basePath' => 'application.res',
                    'baseUrl' => Yii::app()->baseUrl . '/res/lib/font-awesome/',
                    'css' => array('/css/font-awesome.min.css'),
                )
            );
        endif;
    
        $cs->registerPackage('fontAwesome');
    }
    
    public function registerGoogleAnalytics()
    {
        if($this->config('settings_google_analytics_id')){
            Yii::app()->clientScript->registerScript('GA',"
                (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
                ga('create', '".Yii::app()->params['cdn']."', '{$_SERVER['SERVER_NAME']}');
                ga('send', 'pageview');
            ");
        }
    }
    

    STEP4:

    call the functions like this in //layouts/main.php

    Yii::app()->getController()->registerDefaults();
    Yii::app()->getController()->registerFontAwesome();
    Yii::app()->getController()->registerGoogleAnalytics();
    
    0 讨论(0)
  • 2020-11-29 15:55

    To include JS and CSS files in a specific view you can do it via controller by passing the parameters false, true, which will include the CSS and JS for, e.g.:

    $this->renderPartial(
        'yourviewname',
        array(
            'model' => $model,
            false,
            true
        )
    );
    
    0 讨论(0)
提交回复
热议问题