YII主题的控制由CThemeManager和CTheme管理,CThemeManager在应用初始化时作为核心组件注册,主题默认路径是app/themes/。
主题配置
由于主题组件在应用初始化时注册,其配置以及很方面,比如在app/themes/下有一个custom1主题
在主配置文件中加入'theme'=>'custom1'键值对,在主题注册的时候会调用 CWebApplication::setTheme($value)初始化主题的名字。
获取主题
主题的获取从控制器渲染视图说起,通过视action方法传递的视图名称获取视图文件的路径
[php]
view plaincopyCController::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 plaincopyCTheme::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 plaincopypublic 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。