Principles and Practices of the PHP template engine

Source: Internet
Author: User
: This article mainly introduces the principles and practices of the PHP template engine. For more information about PHP tutorials, see. Principles of the 0x00 template engine

The template engine uses a series of predefined labels in the template file to replace the native PHP code. by accessing a PHP entry file, a php compilation file replaces the tag and variable in the template according to the conventions. the template file is finally compiled into a PHP file and then displayed in the browser.

Template File

The front-end developer replaces all the data in the front-end code with the tag and variable name agreed upon by the server developer.

PHP entry file

Server developers inject the variables required in the front-end code into the front-end.

PHP compilation file

This file is the core of the template engine. Here we defineTagStatementBy reading the template file, use a regular expression to match the tags and variables agreed in the template file with the background, and replace the tags and variables with the PHP code, finally, a PHP file that combines the front and back ends is generated.

0x01 agreed tag

In the PHP syntax, includingif...elseforeachAnd common variables to be replaced.$valueNative PHP statements, comments, and so on. Generally, you are used to the following labels:

{$ Value} // corresponding to native
  
{Foreach $ array} {V} {/foreach} // corresponding to the native
  $ V) {echo $ V ;}?>
{If $ data = 'xiaoming'} I 'mxiaoming; {elseif $ data = 'xiaohong '} I 'mxiaohong; {else} I 'mxiaoli; {/if} // corresponding to native
  

For more information, seeSmartyDiscuz.

0x02 construct regular expressions to match tags and variablesGetting started with regular expressions in 30 minutes

For regular expressions, you can stamp them into the previous tutorial, which is easy to use.
The following is a regular expression of the relevant tag.

// Matched regular expression $ this-> T_P [] = "# \{\\$ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f -\ xff] *) \}#"; // match the common variable $ this-> T_P [] = "# \ {foreach \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f -\ xff] *) \}# "; // match {foreach $ array} $ this-> T_P [] =" #\{\/(foreach | if )\}#"; // match {/foreach} or {/if} // The corresponding replacement content $ this-> T_R [] ="
  Value ['\ 1'];?> "; $ This-> T_R [] ="
  Value ['\ 1'] as \ $ K = >\$ V) {?> "; $ This-> T_R [] ="
  ";

0x03 compile the template file

Compile is to read the template file, use a regular expression to replace the template tag and variable, and finally save the replaced content in a php file.

Related functions used:


  

0x04 ends and declares

Through these three steps, a simple template engine has been created successfully, but the working principle of the template engine has been described above. in the process of regular expression matching and replacement, the efficiency is extremely low, the efficiency of PHP itself is inherently low. with regular matching, you can imagine it. Therefore, the template engine generally has its own caching mechanism to save successfully parsed content as an html file and set the cache validity period, which can greatly improve the efficiency.

Statement

This article describes the principles and practices of the PHP template engine in Chapter 5.
Thank you!

The above introduces the principles and practices of the PHP template engine, including some content, and hopes to help those who are interested in the PHP Tutorial.

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.