search
HomePHP FrameworkThinkPHPHow to make templates in thinkphp framework

ThinkPHP is a very popular PHP development framework. It has been widely recognized by developers for its efficient performance, convenient operation and complete documentation. Among them, ThinkPHP's template engine is an important part of it. This article will explain how to make templates in the ThinkPHP framework from three aspects: basic concepts, usage methods, and precautions.

1. Basic concepts

1.1 What is a template engine

A template engine is a thing that separates display logic and business logic. It is a combination of template files and variables. Tool for outputting documents. In ThinkPHP, we can use the template engine to render variables into HTML files to generate dynamic pages.

1.2 Template engine syntax

ThinkPHP’s built-in template engine syntax is similar to other template engine syntaxes. The following are some commonly used syntaxes:

Variable output: {$var}

Call PHP function: {:date('Y-m-d',time())}

Delimiter: The content between "{" and "}" is all template engine can The content of the explanation.

Inherit template: {extend name="Base/base"}

Define template block: {block name="content"} .....{/block}

Calling the template block: {block name="content"} is the location that replaces the previously defined template block. {/block}

1.3 Template layout

ThinkPHP advocates "template layout", that is, dividing the frame and style of the entire page into several files. Here we take the layout file base.html and the content file index.html as examples to demonstrate how to combine the layout file and content file and output them to the browser.

2. How to use

Before using the ThinkPHP template engine, we need to create a new view folder in the project and specify how to use the template engine in the configuration file. Specific examples are as follows:

2.1 Create a new view folder

In ThinkPHP projects, we need to create a new view folder in the root directory to store template files, generally named "view ” or “template”. The directory structure of the view folder can be divided according to your own habits.

For example, we create a new Home folder under the view folder, then create a new Index folder in Home, and create two template files, index.html and base.html.

2.2 Template rendering

ThinkPHP provides a variety of ways to render templates. For example, the value returned in the controller contains the template file name, and the framework will automatically find the specified template file and render the result. .

In the index method of the Index controller, we can return the following data for rendering:

public function index(){
    $this->assign('title','博客首页');
    $this->assign('content','这里是博客的首页!');
    return $this->fetch();
}

At this time, the framework will automatically render the view/Home/Index/index.html template file.

2.3 Template inheritance

In ThinkPHP, we can achieve code reuse through template inheritance, that is, using the base template base.html, other templates inherit it and add it to the base template. Make modifications on the basis.

In the Index template, we need to inherit the base.html template. The inheritance syntax is as follows:

{extend name="Home/base" /}

After the inheritance is successful, we can use the block syntax in the template file to replace the base.html Content, that is, use {block name='content'}...{/block} to occupy the area.

{extend name="Home/base" /}
{block name="content"}

{$title}

{$content}

{/block}

3. Notes

When using the ThinkPHP template engine, you also need to pay attention to the following points:

3.1 File naming convention

In ThinkPHP , the naming of template files needs to follow the following specifications:

Controller name/method name/template name.html

For example, in the Index controller, we need to call the load.html template and name it Should be "Index/load.html".

3.2 Code Comments

When writing template code, we recommend using appropriate comments so that the cause can be found more easily when looking for problems. ThikPHP's comment format is the same as HTML comment format.

<!-- 这里是注释 -->
<div>
    <h1 id="这里是标题">这里是标题</h1>
    <p>这里是内容</p>
</div>

3.3 Template code indentation

The indentation of template code is not necessary, but good indentation can improve readability and make the code more intuitive. Instead of cramming the entire code into one line, split them into appropriate lines to make them easier to read.

<div>
    <h1 id="这里是标题">这里是标题</h1>
    <p>这里是内容</p>
</div>

Summary

This article takes ThinkPHP as an example to explain the basic concepts, usage and precautions of the template engine. I hope this article can provide some reference for readers to understand how to make templates in the ThinkPHP framework.

The above is the detailed content of How to make templates in thinkphp framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is the difference between think book and thinkpadWhat is the difference between think book and thinkpadMar 06, 2025 pm 02:16 PM

This article compares Lenovo's ThinkBook and ThinkPad laptop lines. ThinkPads prioritize durability and performance for professionals, while ThinkBooks offer a stylish, affordable option for everyday use. The key differences lie in build quality, p

How to prevent SQL injection tutorialHow to prevent SQL injection tutorialMar 06, 2025 pm 02:10 PM

This article explains how to prevent SQL injection in ThinkPHP applications. It emphasizes using parameterized queries via ThinkPHP's query builder, avoiding direct SQL concatenation, and implementing robust input validation & sanitization. Ad

How to deal with thinkphp vulnerability? How to deal with thinkphp vulnerabilityHow to deal with thinkphp vulnerability? How to deal with thinkphp vulnerabilityMar 06, 2025 pm 02:08 PM

This article addresses ThinkPHP vulnerabilities, emphasizing patching, prevention, and monitoring. It details handling specific vulnerabilities via updates, security patches, and code remediation. Proactive measures like secure configuration, input

How to install the software developed by thinkphp How to install the tutorialHow to install the software developed by thinkphp How to install the tutorialMar 06, 2025 pm 02:09 PM

This article details ThinkPHP software installation, covering steps like downloading, extraction, database configuration, and permission verification. It addresses system requirements (PHP version, web server, database, extensions), common installat

How to fix thinkphp vulnerability How to deal with thinkphp vulnerabilityHow to fix thinkphp vulnerability How to deal with thinkphp vulnerabilityMar 06, 2025 pm 02:04 PM

This tutorial addresses common ThinkPHP vulnerabilities. It emphasizes regular updates, security scanners (RIPS, SonarQube, Snyk), manual code review, and penetration testing for identification and remediation. Preventative measures include secure

How can I use ThinkPHP to build command-line applications?How can I use ThinkPHP to build command-line applications?Mar 12, 2025 pm 05:48 PM

This article demonstrates building command-line applications (CLIs) using ThinkPHP's CLI capabilities. It emphasizes best practices like modular design, dependency injection, and robust error handling, while highlighting common pitfalls such as insu

How to use thinkphp tutorialHow to use thinkphp tutorialMar 06, 2025 pm 02:11 PM

This article introduces ThinkPHP, a free, open-source PHP framework. It details ThinkPHP's MVC architecture, features (routing, database interaction), advantages (rapid development, ease of use), and disadvantages (potential over-engineering, commun

Detailed steps for how to connect to the database by thinkphpDetailed steps for how to connect to the database by thinkphpMar 06, 2025 pm 02:06 PM

This guide details database connection in ThinkPHP, focusing on configuration via database.php. It uses PDO and allows for ORM or direct SQL interaction. The guide covers troubleshooting common connection errors, managing multiple connections, en

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),