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

In-depth analysis of ThinkPHP5 setting template path

Mar 06, 2020 am 10:21 AM
thinkphp template path parse

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

<?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;,
    ],
];
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

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

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;,
],
Copy after login

apps/admin/config.php file

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

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Detailed explanation of Oracle error 3114: How to solve it quickly Detailed explanation of Oracle error 3114: How to solve it quickly Mar 08, 2024 pm 02:42 PM

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Analysis of the meaning and usage of midpoint in PHP Analysis of the meaning and usage of midpoint in PHP Mar 27, 2024 pm 08:57 PM

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Parsing Wormhole NTT: an open framework for any Token Parsing Wormhole NTT: an open framework for any Token Mar 05, 2024 pm 12:46 PM

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles