echome

关于TP框架设置模板和主题的思考

这是onethink下部署情况

1 /* 主题设置 */
2 \'DEFAULT_THEME\' =>  \'theme\',  // 默认模板主题名称
3 /* 模板目录设置 */
4 \'view_path\'     => \'Public/theme/\',
这里是设置主题,和默认模板目录,当然这是基于thinkphp3.2版本下的配置文件处理方式,其中模板目录默认只能部署在public目录下
这个可能是和入口文件有关系的,目前我还不是那么明白,待研究……

分析后,发现定位模板与主题的路径规则,是否设置view_path为条件,如果设置,就是修改默认路径了,view+theme+controller+functionname.html模式
这就是定位新的模板地址,迁移模板部署在public下代码,
其中:注意下面方法中21行到24行代码,就是引入模板路径的关键代码……
 1 /**
 2  * 自动定位模板文件
 3  * @access protected
 4  * @param string $template 模板文件规则
 5  * @return string
 6  */
 7 public function parseTemplate($template=\'\') {
 8     if(is_file($template)) {
 9         return $template;
10     }
11     $depr   =   C(\'TMPL_FILE_DEPR\');
12     $template = str_replace(\':\', $depr, $template);
13     // 获取当前主题名称
14     $theme = $this->getTemplateTheme();
15 
16     // 获取当前模块
17     $module   =  MODULE_NAME;
18     if(strpos($template,\'@\')){ // 跨模块调用模版文件
19         list($module,$template)  =   explode(\'@\',$template);
20     }
21     // 获取当前主题的模版路径
22     if(!defined(\'THEME_PATH\')){
23         if(C(\'VIEW_PATH\')){ // 视图目录
24             define(\'THEME_PATH\',   C(\'VIEW_PATH\').$module.\'/\'.$theme);
25         }else{ // 模块视图
26             define(\'THEME_PATH\',   APP_PATH.$module.\'/\'.C(\'DEFAULT_V_LAYER\').\'/\'.$theme);
27         }
28     }
29 
30     // 分析模板文件规则
31     if(\'\' == $template) {
32         // 如果模板文件名为空 按照默认规则定位
33         $template = CONTROLLER_NAME . $depr . ACTION_NAME;
34     }elseif(false === strpos($template, $depr)){
35         $template = CONTROLLER_NAME . $depr . $template;
36     }
37     $file   =   THEME_PATH.$template.C(\'TMPL_TEMPLATE_SUFFIX\');
38     if(C(\'TMPL_LOAD_DEFAULTTHEME\') && THEME_NAME != C(\'DEFAULT_THEME\') && !is_file($file)){
39         // 找不到当前主题模板的时候定位默认主题中的模板
40         $file   =   dirname(THEME_PATH).\'/\'.C(\'DEFAULT_THEME\').\'/\'.$template.C(\'TMPL_TEMPLATE_SUFFIX\');
41     }
42     return $file;
43 }

 


当然thinkphp5的配置修改了,所以设置要相应的调整,配置文件中设置public目录下模板路径theme
1  \'template\' => [
2  // 模板根路径
3      \'view_path\' => ROOT_PATH . \'public\' . DS . \'theme\' .DS ,
4     \'view_base\' => ROOT_PATH . \'public\' . DS . \'theme\' .DS,
5     \'view_theme\' => \'default\',//设置主题,自己添加的
6 ],

注意:这里的ROOT_PATH常量实在入口文件定义的,

1 //定义项目根目录
2 define(\'ROOT_PATH\', dirname(__DIR__).\'/\');

 

而在框架目录下  thinkphp\thinkphp\library\think\view\Think.php中

 1 /**
 2      * 自动定位模板文件
 3      * @access private
 4      * @param string $template 模板文件规则
 5      * @return string
 6      */
 7     private function parseTemplate($template)
 8     {
 9         // 分析模板文件规则
10         $request = Request::instance();
11         // 获取视图根目录
12         if (strpos($template, \'@\')) {
13             // 跨模块调用
14             list($module, $template) = explode(\'@\', $template);
15         }
16         if ($this->config[\'view_base\']) {
17             // 基础视图目录
18             $module = isset($module) ? $module : $request->module();
19             $path   = $this->config[\'view_base\'] . ($module ? $module . DS : \'\');
20         } else {
21             $path = isset($module) ? APP_PATH . $module . DS . \'view\' . DS : $this->config[\'view_path\'];
22         }
23 
24         /*springphp 2017-06-08 */
25         if ($this->config[\'view_theme\']) {
26             $path .= $this->config[\'view_theme\'].DS;
27         }
28 
29         $depr = $this->config[\'view_depr\'];
30         if (0 !== strpos($template, \'/\')) {
31             $template   = str_replace([\'/\', \':\'], $depr, $template);
32             $controller = Loader::parseName($request->controller());
33             if ($controller) {
34                 if (\'\' == $template) {
35                     // 如果模板文件名为空 按照默认规则定位
36                     $template = str_replace(\'.\', DS, $controller) . $depr . $request->action();
37                 } elseif (false === strpos($template, $depr)) {
38                     $template = str_replace(\'.\', DS, $controller) . $depr . $template;
39                 }
40             }
41         } else {
42             $template = str_replace([\'/\', \':\'], $depr, substr($template, 1));
43         }
44         return $path . ltrim($template, \'/\') . \'.\' . ltrim($this->config[\'view_suffix\'], \'.\');
45     }
注意设置上面24行至27行的代码,即可调用主题,设置就是这么简单吧,
当然tp5中模块化设计更加灵活,所有若是在公共配置文件中修改以上代码,会出现,多模块都应用了主题设置,这样显然不是我们要的,这个时候,我们可以考虑单独设置模块主题,如下面在继承controller控制器的控制器下加入如下代码:
1  public function __construct(){
2     $this->theme = config(\'web_theme\')?:(config(\'template.view_theme\')?:\'default\');
3     config(\'template.view_theme\',$this->theme);
4  }
5 //如果没有设置view_theme 就是默认的主题default.

初始化就设置了默认主题,在前台控制器设置即可单独处理前台的主题而不影响后台模板设计……

   说明;这样可以修改模板路径,但是tp5去掉了设置主题的功能,很可惜,但是可以自己修改添加主题即可,如上面代码,就是很简单的一种实现了……

  注意:初学者都应该知道,tp框架下view视图的渲染,在框架下view目录中的Think.php类中加载处理的……

还有个小问题就是使用 view()助手函数时传递参数的优化

1 public function index(){
2    $info = model(\'user\')->select();
3    return view([
4             \'info\'  =>$info,
5    ]);
6 }

这个上面的代码在tp5里是会报错的,因为助手函数的参数问题,在tp框架library目录下helper.php助手函数文件中,默认的格式是有$template

1 function view($template = \'\',$vars = [], $replace = [], $code = 200)
2 {
3    if (is_array($template)) {
4        $vars = $template;
5        $template = \'\';
6     }
7     return Response::create($template, \'view\', $code)->replace($replace)->assign($vars);
8 }

默认会要求输入$template参数的,这样显得繁琐,如果加了3行至6行代码处理下就可以简化了,是不是很简单,呵呵,希望能给初学的朋友有所帮助!

 

分类:

技术点:

相关文章:

  • 2021-09-28
  • 2021-08-12
  • 2021-09-30
  • 2021-09-30
  • 2021-11-16
  • 2021-12-09
猜你喜欢
  • 2021-11-07
  • 2021-11-07
  • 2021-11-12
  • 2021-12-31
  • 2022-01-02
  • 2018-01-09
  • 2021-09-28
相关资源
相似解决方案