不喜勿喷,需要修改框架两个文件约20行代码
TP5 view_ba
修改为统一思路,支持控制器渲染时不指定模板名称自动定位,支持跨模块 @ 调用
修改后实现以下视图模板结构需求:
view_ba
'template' => [
'type' => 'Think',
'view_base' => '',// 前后不带DS
'view_path' => 'view',// 前后不带DS
'view_theme' => '',// pc mobile 前后不带DS
'view_suffix' => 'html',
'view_depr' => DS
],[全局视图层目录] 定义view_baApplication\(view_ba
------------(module_name1)\[(view_theme)]\
-------------------(controller1)\(action).html
-------------------(controller2)\(action).html
------------(module_name2)\[(view_theme)]\
-------------------(controller1)\(action).html
-------------------(controller2)\(action).html
[相对视图层目录] 定义view_path,把view_ba
Application\
------------(module_name1)\(view_path)\[(view_theme)]\
-------------------(controller1)\(action).html
-------------------(controller2)\(action).html
------------(module_name2)\(view_path)\[(view_theme)]\
-------------------(controller1)\(action).html
-------------------(controller2)\(action).html
view_ba
view_theme 可选,默认为空
[1]、关于配置项
增加一个view_theme的配置项
view_ba
view_path 配置:默认'view' ,相对视图层目录(module_name下视图层文件夹,前后不带DS)
view_theme 配置:默认'' ,主题相对目录(view_path下主题文件夹,前后不带DS)
如果配置了view_ba
如果未配置view_ba
[2]、用途
如需实现多套模板主题,控制器基类接受(或检测判断)、缓存主题名称,在控制器fetch或view()渲染试图前动态设置 view_theme 即可实现
$this->view->config('view_theme', 'pc');
$this->view->config('view_theme', 'mobile');
$this->view->config('view_theme', 'blue');
$this->view->config('view_theme', 'red');
如不适用模板主题,把config 配置文件view_theme默认配置为空就行注意:如使用多主题,建议config统一配置view_theme或动态配置view_theme,调用fetch()渲染时不带 view_theme 主题名称
TP控制器渲染
return $this->fetch();
return $this->fetch('aa/bb');
return $this->fetch('module_name@aa/bb');自定义渲染$View=View::instance();
return $View->fetch('common@form_build/layout-form',['form_data'=>$form_data],[]);[2.1]、相对视图层+(view_path)+[view_theme]使用相对视图层, 把 config 配置文件view_ba
[2.2]、全局视图层+[view_theme]
使用全局视图层, 把 config 配置文件view_ba
config 配置文件统一约定后,可以直接实现全局视图层目录(view_ba
启用全局视图层后,view_path就不需要了,还可以加入view_theme实现多套主题
[修改TP5文件]
[文件一] think\Template
private function parseTemplateFile($template){
.......
//[START] 模板定位修改
if (!isset($this->config['view_path']) || empty($this->config['view_path'])) {
$this->config['view_path'] = 'view';
}
if (!isset($this->config['view_theme'])) {
$this->config['view_theme'] = '';
}
$module = isset($module) ? $module : Request::instance()->module();
if (isset($this->config['view_base']) && !empty($this->config['view_base'])) {
//使用全局视图层目录
$path = APP_PATH . $this->config['view_base'] . DS . $module . DS;
} else {
//使用相对视图层目录
$path = APP_PATH . $module . DS . $this->config['view_path'] . DS;
}
!empty($this->config['view_theme']) && $path .= $this->config['view_theme'] . DS;
//[END] 模板定位修改
.......
}[文件二] think\view\driver\Think //架构函数,注释自动设置view_path为绝对地址
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
/*
if (empty($this->config['view_path'])) {
$this->config['view_path'] = App::$modulePath . 'view' . DS;
}
*/
$this->template = new Template($this->config);
}
private function parseTemplate($template){
.......
//[START] 模板定位修改
if (!isset($this->config['view_path']) || empty($this->config['view_path'])) {
$this->config['view_path'] = 'view';
}
if (!isset($this->config['view_theme'])) {
$this->config['view_theme'] = '';
}
$module = isset($module) ? $module : $request->module();
if (isset($this->config['view_base']) && !empty($this->config['view_base'])) {
//使用全局视图层目录
$path = APP_PATH . $this->config['view_base'] . DS . $module . DS;
} else {
//使用相对视图层目录
$path = APP_PATH . $module . DS . $this->config['view_path'] . DS;
}
!empty($this->config['view_theme']) && $path .= $this->config['view_theme'] . DS;
//[END] 模板定位修改
.......
} 最佳答案