Home > Article > Backend Development > PHP template engine
The origin of the PHP template engine
● In order to solve a series of problems that occurred in the hybrid development of WEB applications at that time: the code was difficult to maintain, the code could not be reused, and programmers required broad knowledge, etc.
● To achieve incomplete separation of back-end and front-end, developers and artists can work together to improve efficiency
Problems caused by PHP template engine
● Due to incomplete separation, there is still some coupling in the division of labor
● The PHP template engine led to the birth of the template engine language, increased learning costs, and also led to a debate over who should learn between the front-end and the back-end
Does PHP template engine still have value today?
● The current solution for complete front-end and back-end separation has emerged. The three major front-end frameworks (vuejs/React, etc.) have all implemented front-end template engines, so the basic PHP template has been replaced. Reduce the pressure on the server
● But if you want to maintain old projects, you still need to use the PHP template engine. If you want to develop PC projects simply and quickly, using the PHP template engine is a better choice
PHP template engine implementation principle
● Read the content of the template file
$content = file_get_contents($tmplFile);
● Use regular expressions to replace the template language with PHP language
$repContent = preg_replace($pattern, $replacement, $content);
● Save it as a new .php file
file_put_contents($comFile,$repContent);
● Finally include the newly generated .php file
include $comFilePath;
Notes on include and require
● The function of include and require is to use the following string as the file name. Regardless of whether the file extension is .php or not, the file is considered to be a PHP program and is introduced into the current program to run.
● If the php program is not wrapped by 36aafd3c2422d8273bebb683d205fd9f, it will be output directly.
● When a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that needs to be executed as PHP code must be included within valid PHP start and end tags.
Recommended: "PHP Tutorial"
The above is the detailed content of PHP template engine. For more information, please follow other related articles on the PHP Chinese website!