打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
YII框架分析笔记12:主题管理
YII主题的控制由CThemeManager和CTheme管理,CThemeManager在应用初始化时作为核心组件注册,主题默认路径是app/themes/。
主题配置
由于主题组件在应用初始化时注册,其配置以及很方面,比如在app/themes/下有一个custom1主题
在主配置文件中加入'theme'=>'custom1'键值对,在主题注册的时候会调用 CWebApplication::setTheme($value)初始化主题的名字。
获取主题
主题的获取从控制器渲染视图说起,通过视action方法传递的视图名称获取视图文件的路径
[php] view plaincopy
CController::getViewFile()
public function getViewFile($viewName)
{
if(($theme=Yii::app()->getTheme())!==null && ($viewFile=$theme->getViewFile($this,$viewName))!==false)
return $viewFile;
$moduleViewPath=$basePath=Yii::app()->getViewPath();
if(($module=$this->getModule())!==null)
$moduleViewPath=$module->getViewPath();
return $this->resolveViewFile($viewName,$this->getViewPath(),$basePath,$moduleViewPath);
}
[php] view plaincopy
CTheme::etViewFile()
/**
* Finds the view file for the specified controller's view.
* @param CController $controller the controller
* @param string $viewName the view name
* @return string the view file path. False if the file does not exist.
*/
public function getViewFile($controller,$viewName)
{
$moduleViewPath=$this->getViewPath();  //获取视图目录路径,默认是app/themes/custom1/view
if(($module=$controller->getModule())!==null)
$moduleViewPath.='/'.$module->getId(); //如果存在module,目录路径app/themes/custom1/view/moduleId
return $controller->resolveViewFile($viewName,$this->getViewPath().'/'.$controller->getUniqueId(),$this->getViewPath(),$moduleViewPath);
}
回到CController获取具体视图文件路径,视图名称有三种特殊命名
1、以//开头是相对于应用级别的视图路径
2、以/开头是相对于module基本的视图路径
3、以xx.xx是别名视图
[php] view plaincopy
public function resolveViewFile($viewName,$viewPath,$basePath,$moduleViewPath=null)
{
if(empty($viewName))
return false;
if($moduleViewPath===null)
$moduleViewPath=$basePath;
if(($renderer=Yii::app()->getViewRenderer())!==null)
$extension=$renderer->fileExtension;
else
$extension='.php';
if($viewName[0]==='/')
{
if(strncmp($viewName,'//',2)===0)
$viewFile=$basePath.$viewName;
else
$viewFile=$moduleViewPath.$viewName;
}
else if(strpos($viewName,'.'))
$viewFile=Yii::getPathOfAlias($viewName);
else
$viewFile=$viewPath.DIRECTORY_SEPARATOR.$viewName;
if(is_file($viewFile.$extension))
return Yii::app()->findLocalizedFile($viewFile.$extension);
else if($extension!=='.php' && is_file($viewFile.'.php'))
return Yii::app()->findLocalizedFile($viewFile.'.php');
else
return false;
}
在视图文件中,在对该主题在的样式js和图片的引用的需要用到Yii::app()- >theme->baseUrl来作为baseUrl。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Yii PHP 框架分析(四)
Yii小部件(Widget)
yii框架的使用
Yii使用数据库操作
Yii中同时连接多个数据库 ? Hacklog
Yii 框架中基于角色的访问控制-rbac
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服