zoukankan      html  css  js  c++  java
  • [Yii Framework] How to embed the css and images in a module

    Maybe there is not a good introduction about how to embed css and images in a module in the forum (or cookbook) of Yii framework, but thanks to Qiang, who builds the great framework, that I found the way in the kernal code in framework/gii (In fact that gii is a module!)

    Here is the structure of my module "admin"

    代码
    | | |~modules/
    | | | `~admin/
    | | | |~assets/
    | | | | |+css/
    | | | | |+images/
    | | | |+components/
    | | | |+controllers/
    | | | |+messages/
    | | | |+models/
    | | | |+views/
    | | | `-AdminModule.php*

    1. Modify the AdminModule.php, add the code as below:

    代码
    private $_assetsUrl;

    /**
    * @return string the base URL that contains all published asset files of this module.
    */
    public function getAssetsUrl()
    {
    if($this->_assetsUrl===null)
    $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('admin.assets'));
    return $this->_assetsUrl;
    }

    /**
    * @param string the base URL that contains all published asset files of this module.
    */
    public function setAssetsUrl($value)
    {
    $this->_assetsUrl=$value;
    }

    public function registerCss($file, $media='all')
    {
    $href = $this->getAssetsUrl().'/css/'.$file;
    return '<link rel="stylesheet" type="text/css" href="'.$href.'" media="'.$media.'" />';
    }

    public function registerImage($file)
    {
    return $this->getAssetsUrl().'/images/'.$file;
    }

    2. Example of calling the css and images in modules/admin/layouts/main.php

    代码
    //calling the css
    <?php echo Yii::app()->controller->module->registerCss('main.css');?>

    //calling the images
    <?php echo CHtml::image(Yii::app()->controller->module->registerImage('logo.png'), "logo");?>

    Have fun with Yii!

  • 相关阅读:
    JQuery性能优化
    分页控件X.PagedList.Mvc
    《转》sql 、linq、lambda 查询语句的区别
    Linq的连表查询
    MVC页面直接F5出错
    详解集合
    Json的序列化与反序列化
    《转》dbcontext函数
    《转》jquery中的$.ajax的success与error
    cocos creator基础-(二十七)httpclient Get POST
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1833005.html
Copyright © 2011-2022 走看看