Include CSS,javascript file in Yii Framework

前端 未结 18 1714
[愿得一人]
[愿得一人] 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:57

    Do somthing like this by adding these line to your view files;

    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/javascript/file');
    Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/path/to/css/file');
    
    0 讨论(0)
  • 2020-11-29 15:59

    You can do so by adding

    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/script');
    
    0 讨论(0)
  • 2020-11-29 16:00

    I liked to answer this question.

    Their are many places where we have css & javascript files, like in css folder which is outside the protected folder, css & js files of extension & widgets which we need to include externally sometime when use ajax a lot, js & css files of core framework which also we need to include externally sometime. So their are some ways to do this.

    Include core js files of framework like jquery.js, jquery.ui.js

    <?php 
    Yii::app()->clientScript->registerCoreScript('jquery');     
    Yii::app()->clientScript->registerCoreScript('jquery.ui'); 
    ?>
    

    Include files from css folder outside of protected folder.

    <?php 
    Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/example.css');
    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/example.js');
    ?>
    

    Include css & js files from extension or widgets.

    Here fancybox is an extension which is placed under protected folder. Files we including has path : /protected/extensions/fancybox/assets/

    <?php
    // Fancybox stuff.
    $assetUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.fancybox.assets'));
    Yii::app()->clientScript->registerScriptFile($assetUrl.'/jquery.fancybox-1.3.4.pack.js'); 
    Yii::app()->clientScript->registerScriptFile($assetUrl.'/jquery.mousewheel-3.0.4.pack.js'); 
    ?>  
    

    Also we can include core framework files: Example : I am including CListView js file.

    <?php
    $baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets'));
    Yii::app()->clientScript->registerScriptFile($baseScriptUrl.'/listview/jquery.yiilistview.js',CClientScript::POS_END);  
    ?>
    
    • We need to include js files of zii widgets or extension externally sometimes when we use them in rendered view which are received from ajax call, because loading each time new ajax file create conflict in calling js functions.

    For more detail Look at my blog article

    0 讨论(0)
  • 2020-11-29 16:01

    Add the CSS and JS in The Layout Folder.Access anywhere in the project

      <!--// Stylesheets //-->
        <?php
            $themepath=Yii::app()->theme->baseUrl;
            Yii::app()->clientScript->registerCoreScript("jquery");
        ?>
    
            <link href="<?php echo $themepath."/css/custom.css"; ?>" rel="stylesheet" media="all" />
    
    
    <!--// Javascript //-->
    <?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
    </script> -->
    <script type="text/javascript" src="<?php echo $themepath; ?>/js/video.min.js"></script>
    
    0 讨论(0)
  • 2020-11-29 16:01
    • In Yii Assets are declared in engine/assets/Appassets.php This make more easier to manage all your css and js files
    0 讨论(0)
  • 2020-11-29 16:04

    In Yii framework, You can include js and css using below method.

    Including CSS:

    {Yii::app()->request->baseUrl}/css/styles.css
    

    Including JS:

    {Yii::app()->request->baseUrl}/js/script.js
    

    Including Image:

    {Yii::app()->request->baseUrl}/images/logo.jpg
    

    Note: By using layout concept in yii, You can add css and js instead of specifying in view template.

    0 讨论(0)
提交回复
热议问题