Manually develop a PHP template engine (35) and a php template. Manual development of the PHP template engine (35), the php template is called TPL, imitating the smarty template engine. The template we call is a Web template. it is mainly written in a language consisting of HTML tags. it is a manual PHP template engine (35) and a php template.
The template is called TPL, which imitates the smarty template engine.
The template we call is a Web template. it is a page written in a language consisting of HTML tags. However, there are also ways to express the dynamically generated content (parsing tags ). The template engine is a software library that allows us
Generate HTML code from the template and specify the dynamic content to be included.
1. features of the template engine:
1. encourage separation: improve the readability and maintainability of more systems.
2. promote the division of labor: enable programmers and artists to focus on their own designs.
3. easier parsing than PHP: compilation files and cache files are loaded faster and consume less resources.
4. Increased Security: The template designer's ability to perform insecure operations can be restricted to avoid accidental access by mistake.
2. Template engine products:
PHP has many Templates developed by teams, such as Smarty, Heyes Templates Class,
FastTemplate. These template engines can be used directly to fully implement the above
Features. However, for beginners, understanding the principles of the template engine can help them better understand why the model is used.
Board.
3 TPL template flowchart
When we create a template engine ourselves, the biggest benefit is simplicity. Because many teams have compiled a template engine, although it has many powerful functions, it is highly secure. However, the disadvantage is that many of them are not available, and they are very small.
.
4. create a TPL template engine 1. create folders and files required for the initial template
A) index. php main file for compiling business logic.
B) template. inc. php template initialization file for initial template information.
C) the templates Directory stores all template files.
D) the templates_c Directory stores all the compiled files.
E) the cache Directory stores all cached files.
F) the classes Directory stores all class files.
G) the config Directory stores the template system variable configuration file.
2. the index. php file code under the root directory of the website
// Set the encoding to utf-8header ('content-Type: text/html; charset = utf-8 '); // The website ROOT directory define ('root _ path ', dirname (_ FILE _); // Save the template folder define ('tpl _ dir', ROOT_PATH. '/templates/'); // compile the folder define ('tpl _ C_DIR ', ROOT_PATH. '/templates_c/'); // CACHE folder define ('cache _ dir', ROOT_PATH. '/cache /');
3. Templates. class. php-added methods to determine whether the directory exists
// Create a constructor public function _ construct () {// verify whether the directory exists if (! Is_dir (TPL_DIR) |! Is_dir (TPL_C_DIR) |! Is_dir (CACHE_DIR) {exit ('Error: Template folder, compilation folder, or cache folder not created! ');}}
4. the Templates. class. php-create the display () method in the Templates folder to load the. tpl template file.
This is also the display prototype method in samrty.
// Import the template to the PHP file. public function display ($ _ file) {// set the path of the template file $ _ tplFile = TPL_DIR. $ _ file; // Determine whether the template file exists if (! File_exists ($ _ tplFile) {exit ('Error: The template file does not exist! ');} // Set the name of the compiled file $ _ parFile = TPL_C_DIR.md5 ($ _ file ). $ _ file. '. php '; // Determine whether the compiled file exists and whether the template file has been modified if (! File_exists ($ _ parFile) | filemtime ($ _ parFile) <filemtime ($ _ tplFile) {// Generate the compilation file file_put_contents ($ _ parFile, file_get_contents ($ _ tplFile);} // load the compilation file include $ _ parFile;} // introduce the template class require ROOT_PATH. '/includes/Template. class. php '; // instantiate the Template class $ _ tpl = new Template (); // load index. tpl $ _ tpl-> display ('index. tpl );
To be continued
Listen 1 (35), php template is called TPL, imitating the smarty template engine. The template we call is a Web template, which is mainly written in a language consisting of HTML tags...