Home> PHP Framework> ThinkPHP> body text

In-depth analysis of ThinkPHP5 setting template path

angryTom
Release: 2020-03-06 10:21:17
forward
5128 people have browsed it

This article introduces the method of setting the front-end template path and back-end template path in thinkphp. It has certain reference value. I hope it will be helpful to friends who are learning thinkPHP!

In-depth analysis of ThinkPHP5 setting template path

In-depth analysis of ThinkPHP5 setting template path

The default template path is in the module/view file. If you think this is not convenient to manage and want to set it in the Template directory, you can do so.

Template parameters, other parameters that can be affected are the config.php template->view_path parameters under the current module.

Practical operation

(Recommended tutorial:thinkphp tutorial)

1. Configure shared parameters

Set some parameters in apps/config.php to facilitate calling config.php under the Index or Admin module.

apps/config.php, add some parameters.

'template' => [// 模板路径 'view_path' => 'template/', // 就是这里 /** * 前台文件配置 * Author: MR.zhou * */ 'index' => [ // 模快名称 'model_name' =>'index', // 默认模板文件名称 'default_template' => 'default', // 这里可以切换模块下的默认模板名称 ], /** * 后台文件配置 * Author: MR.zhou * */ 'admin'=>[ // 模快名称 'model_name' =>'admin', // 默认模板文件名称 'default_template' =>'default', // 这里可以切换模块下的默认模板名称 ],
Copy after login

2. Set template parameters

index/config.php

'template' => [ // 模板路径 'view_path' => config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/', ],
Copy after login

admin/config.php

 [ // 模板路径 'view_path' => config('template.view_path').config('admin.model_name').'/'.config('index.default_template').'/', ], ];
Copy after login

3. Setting parameter analysis

The above are the configuration parameters given by others on the thinkPHP official website, but are you unable to display the page correctly according to the above configuration? There are several misunderstandings here that you need to understand. First, view_path =>'template/' in the shared parameter configuration file actually defines the template file as template. However, some people only want to define the template path on the front end, and still use the default view in the background. template method. But such a setting also defines the background template path. How should we solve this problem? The editor below gives two ways to solve the problem of only defining the front-end template path but not the back-end

The first: Do not define the template path in the shared configuration file, but define it in the index module Template path, so it has nothing to do with the background

apps/index/config.php file

'template' => [ // 模板路径 'view_path' => 'template/'.config('index.default_template').'/', ],
Copy after login

Of course, the index configuration file is still defined in my shared configuration file, which is the same as template level, instead of putting it in the template

apps/config.php file

/** * 前台文件配置 * Author: MR.zhou * */ 'index' => [ // 模快名称 'model_name' =>'index', // 默认模板文件名称 'default_template' => 'default', // 这里可以切换模块下的默认模板名称 ],
Copy after login

The second type: Define the template path in the shared configuration file as template, and define the template path in the index module, and redefine view_path in the background =>''

apps/config.php file

/** * 前台文件配置 * Author: MR.zhou * */ 'index' => [ // 模快名称 'model_name' =>'index', // 默认模板文件名称 'default_template' => 'default', // 这里可以切换模块下的默认模板名称 ], 'template' => [// 模板路径 'view_path' => 'template/', // 就是这里
Copy after login

The current background configuration files are as follows

apps/index/config.php文件 'template' => [ // 模板路径 'view_path' => config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/', ],
Copy after login

apps/admin/config.php file

'template' => [ // 模板路径 'view_path' => '', ],
Copy after login

For more thinkPHP tutorials, please pay attention toPHP Chinese website!

The above is the detailed content of In-depth analysis of ThinkPHP5 setting template path. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.100txy.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!