[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!

转载于:https://www.cnblogs.com/davidhhuan/archive/2010/09/22/1833005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值