Home > PHP Framework > ThinkPHP > body text

How to operate template engine in ThinkPHP6?

WBOY
Release: 2023-06-12 09:21:09
Original
1474 people have browsed it

In ThinkPHP6, the template engine is a very important part, it can help us render views and display data more efficiently. This article will introduce how to operate the template engine in ThinkPHP6.

1. Basic knowledge of template engine

  1. Definition of template engine

The template engine is a tool that converts data into HTML. The main function is to separate views and business logic. Normally, we process data and views separately, then combine the two through a template engine and finally present them to the user.

  1. Classification of template engines

In ThinkPHP6, template engines are mainly divided into two types: one is PHP-based template engine (such as Smarty, Blade, etc.), The other is a template engine based on native syntax.

  1. Advantages of template engine

The template engine can help us separate the view and business logic, improve the maintainability and readability of the code, and can quickly Implement changes to page layout styles and improve development efficiency.

2. Template engine operation in ThinkPHP6

  1. Creation of template file

In ThinkPHP6, we can quickly create a template file through the following command :

php think make:view Index/index
Copy after login

Among them, Index represents the controller name, and index represents the method name. After executing this command, an Index directory will be automatically generated in the application directory, and an index.html file will be created in this directory.

  1. Writing template files

After creating the template file, we can write HTML, CSS, JavaScript and other codes according to our own needs. In the template file, data can also be embedded through the syntax of the template engine.

For example:

<html>
    <head>
        <title>用户列表</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>注册时间</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($users as $user): ?>
                <tr>
                    <td><?php echo $user['id']; ?></td>
                    <td><?php echo $user['username']; ?></td>
                    <td><?php echo $user['email']; ?></td>
                    <td><?php echo $user['create_time']; ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </body>
</html>
Copy after login

In the above code, we use PHP's foreach loop statement to traverse the user list data and render the data into the HTML page.

  1. Assignment and use of template variables

In ThinkPHP6, we can use the assign method in the controller to set variables for the template file.

For example:

public function index()
{
    // 获取用户数据
    $users = Db::name('user')->select();

    // 设置模板变量
    $this->assign('users', $users);

    // 渲染模板输出
    return $this->view->fetch();
}
Copy after login

In the above code, we first obtain the user data through the Db::name('user')->select() method, and then through $this-> The ;assign() method sets data into template variables. Finally, the template file is rendered into an HTML page through the return $this->view->fetch() method and output to the browser.

In the template file, you can obtain the specified variable value through the syntax {{$variable name}}.

For example:

@foreach($users as $user)
<tr>
    <td>{{$user['id']}}</td>
    <td>{{$user['username']}}</td>
    <td>{{$user['email']}}</td>
    <td>{{$user['create_time']}}</td>
</tr>
@endforeach
Copy after login

In the above code, we use the {{$}} syntax to obtain the corresponding value in the user data and display it in the HTML page.

  1. Implementation of template layout

In actual development, we usually use all the common code in the page layout (such as header, tail, sidebar, etc. ) for reuse in other pages.

In ThinkPHP6, we can implement this function by using template layout. The specific operations are as follows:

1) Create a layout directory in the application directory and create a base in this directory .html file.

2) Set the page layout in the base.html file, such as header, tail and other common codes.

For example:

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>{{$title}}</title>
    </head>
    <body>
        <header>
            <!-- 头部代码 -->
        </header>
        <main>
            <!-- 主体代码 -->
        </main>
        <footer>
            <!-- 底部代码 -->
        </footer>
    </body>
</html>
Copy after login

In the above code, we set the basic layout of the HTML page and use the {{$}} syntax to get the variable value.

3) Use extends and section syntax in other template files to inherit and use public layout files.

For example:

@extends('layout/base')

@section('content')
    <table>
        <thead>
            <tr>
                <th>编号</th>
                <th>用户名</th>
                <th>邮箱</th>
                <th>注册时间</th>
            </tr>
        </thead>
        <tbody>
            @foreach($users as $user)
            <tr>
                <td>{{$user['id']}}</td>
                <td>{{$user['username']}}</td>
                <td>{{$user['email']}}</td>
                <td>{{$user['create_time']}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
@endsection
Copy after login

In the above code, we first use the @extends syntax to inherit the public layout file, and then use the @section and @endsection syntax to replace and expand the template content.

Conclusion

Through the introduction of this article, readers should already understand how to operate the template engine in ThinkPHP6, including the creation of template files, the assignment and use of template variables, the implementation of template layout, etc. . Template engine is an important technology in web development. Mastering this technology can improve development efficiency and code maintainability.

The above is the detailed content of How to operate template engine in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template