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"}{/block}{$title}
{$content}
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>这里是标题</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>这里是标题</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!