Home  >  Article  >  PHP Framework  >  In-depth analysis of ThinkPHP5 setting template path

In-depth analysis of ThinkPHP5 setting template path

angryTom
angryTomforward
2020-03-06 10:21:175210browse

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',        // 这里可以切换模块下的默认模板名称
],

2. Set template parameters

index/config.php

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

admin/config.php

<?php
//配置文件
return [
    // 模板配置
    &#39;template&#39;               => [
        // 模板路径
        &#39;view_path&#39;    => config(&#39;template.view_path&#39;).config(&#39;admin.model_name&#39;).&#39;/&#39;.config(&#39;index.default_template&#39;).&#39;/&#39;,
    ],
];

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

&#39;template&#39;               => [
    // 模板路径
    &#39;view_path&#39;    => &#39;template/&#39;.config(&#39;index.default_template&#39;).&#39;/&#39;,
],

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
     * */
    &#39;index&#39; => [
        // 模快名称
        &#39;model_name&#39; =>&#39;index&#39;,
        // 默认模板文件名称
        &#39;default_template&#39; => &#39;default&#39;,       // 这里可以切换模块下的默认模板名称
    ],

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
     * */
    &#39;index&#39; => [
        // 模快名称
        &#39;model_name&#39; =>&#39;index&#39;,
        // 默认模板文件名称
        &#39;default_template&#39; => &#39;default&#39;,       // 这里可以切换模块下的默认模板名称
    ],
    
    'template'               => [// 模板路径
        'view_path'    => 'template/',     // 就是这里

The current background configuration files are as follows

apps/index/config.php文件
&#39;template&#39;               => [
    // 模板路径
    &#39;view_path&#39;    => config(&#39;template.view_path&#39;).config(&#39;index.model_name&#39;).&#39;/&#39;.config(&#39;index.default_template&#39;).&#39;/&#39;,
],

apps/admin/config.php file

&#39;template&#39;               => [
    // 模板路径
    &#39;view_path&#39;  => &#39;&#39;,
],

For more thinkPHP tutorials, please pay attention to PHP 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!

Statement:
This article is reproduced at:www.100txy.com. If there is any infringement, please contact admin@php.cn delete