Manual development of PHP template engine 1 (35), php Template

Source: Internet
Author: User
Tags php template smarty template

Manual development of PHP template engine 1 (35), 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

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.