Manual development of PHP template engine 1 (35)

Source: Internet
Author: User
Tags php template
: This article mainly introduces the manual development of PHP template engine 1 (35). If you are interested in the PHP Tutorial, please refer to it. 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

The above introduces the manual development of PHP template engine 1 (35), including the content, hope to be helpful to friends interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.