zoukankan      html  css  js  c++  java
  • [Yii Framework] How to develop an extension with image, css and js

    Here is an example of how to develop a widget (yes, widget is also an extension in Yii) in Yii.

    Let say you want to display something with widget in the page, of course you can place your widget

    in protected/components when there are one or two classes inside. But when there is a lot of them, 

    it is hard for you to manage them. Therefore we will place them in protected/extensions. Here we

    will build the testWidget, the path will be protected/extensions/testWidget.

    Structure:

    | | | `~testWidget/
    | | |   |~assets/
    | | |   | |~css/
    | | |   | | `-testWidget.css
    | | |   | |~images/
    | | |   | | `-loading.gif
    | | |   | `~js/
    | | |   |   `-testWidget.js
    | | |   |~views/
    | | |   | `-testWidget.php
    | | |   `-testWidget.php

      

    1. Develop a widget class file: testWidget.php

    <?php
    class testWidget extends CWidget
    {
        private $loadingImageUrl;

        public function init()
        {
            $assetsDir = dirname(__FILE__).'/assets';
            $cs = Yii::app()->getClientScript();
            $cs->registerCoreScript("jquery");
            
            // Publishing and registering JavaScript file
            $cs->registerScriptFile(
                Yii::app()->assetManager->publish(
                    $assetsDir.'/js/testWidget.js'
                ),
                CClientScript::POS_END
            );
                    
            // Publishing and registering CSS file
            $cs->registerCssFile(
                Yii::app()->assetManager->publish(
                    $assetsDir.'/css/testWidget.css'
                )
            );
            
            // Publishing image
            $this->loadingImageUrl = Yii::app()->assetManager->publish(
                $assetsDir.'/images/loading.gif'
            );
        }


        public function run()
        {
            $this->render(
                'testWidget', 
                array(
                    'loadingImageUrl' => $this->loadingImageUrl,
                )
            );
        }
    }

    2. Develop the view file:

    <?php echo 'Hello, world!';?>

    3. You can develop you testWidget.css and testWidget.js file. 

    4. Use the new widget.

    <?php $this->widget("ext.testWidget.testWidget")?>

    Yes, that is a very simple widget extension, just a tutorial to show how to build one.

    Have fun with Yii! 

  • 相关阅读:
    写壳笔记一(加节表)
    cs_forums_GetForumsModeratedByUser///cs_forums_GetUnmoderatedPostStatus
    cs_Favorites_Get///cs_Favorites_GetSections
    cs_Feed_UpdateFeedStatus///cs_FeedPost_GetPost///cs_FeedPost_GetPostFullDetails///cs_FeedPost_UpdatePosts
    又来牢骚一下
    cs_Favorite_Delete///cs_Favorite_Get
    cs_Folder_GetFolders///cs_Folder_GetSummary///cs_Folder_MoveFolder
    cs_Folder_RenameFolder///cs_forums_GetForumMessages///cs_forums_GetForumModerators
    cs_Favorites_GetUsers///cs_Favorites_GetUsersWatching
    cs_Feed_AddFeed///cs_Feed_GetAll
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/2316851.html
Copyright © 2011-2022 走看看