PHP Template principle

Source: Internet
Author: User

Recently at the time of development, encountered in the case of HTML templates, the need to generate a daily data report, if the frame is too heavy, it would like to own a simple template to implement, so simple and practical, by the way, record the principle.
I. Principle of the template engine
PHP is a web-embedded language, using 〈?php?〉 tags embedded in the HTML text, and then by the PHP interpreter to execute, dynamically generated pages. But for large-scale Web applications, it is generally divided into front end, background business logic, and data layer. For project collaboration and division of labor, it is necessary to separate the front-end and business logic, the front-end focus on the interface display, the backend program responsible for logical processing. This makes changing the logic content of the program does not affect the front-end staff of the page design, the front-end staff to re-modify the page does not affect the program logic, which in the multi-person cooperation project is particularly important.

How do you connect the two parts after separation? How does the data for the logical layer pass to the template page? What are the rules for dynamic Data items in a template page to be displayed in advance?

This is the need for such a "bridge" to solve this problem, that is, the template engine.

In general, two classes are required to implement the template engine entry class and template parsing class, respectively. mytpl.class.php files typically define a class Mytpl that defines an array tpl_var[] to hold the parameters of the custom content label in tpl.html. MyTpl.Compile.class.php, parsing the template file, is the TPL.HML in the unrecognized content tags into PHP statements, compiled into the "Templates_c" folder under the tpl_c.html.

So the general template reference steps are:

include"Mytpl.class. php file ";//loading the template engine$TPL=NewMYTPL ();//instantiate a template class$title="title";$content="Content";$TPL->assign ("title",$title);//call a method in a template class, assign a variable$TPL->assign ("Content",$content);$TPL->display ("tpl.html");//invoke a method in the template class to display the compiled content

The entire process:

Two. Simple implementation of template engine

The following implements a simplest template engine that does not contain template parsing classes:

tpl.html: < HTML > < Body > <?  ?></><?  ?></body></ HTML  >
mytpl.php:<?PHP//Include includes other file//extract: Import variables from the array into the current symbol table, the key to do the variable, the value of the value! classMYTPL { Public $TPL _vars;  Public $key;  Public $val;  Public functionAssign$TPL _var,$value){                if(Is_array($TPL _var)) {                        foreach($TPL _var  as $_key=$_val) {                                if($_key! = ") {                                        $this->tpl_vars[$_key] =$_val; }                        }                } Else {                        if($TPL _var! = ") {                                $this->tpl_vars[$TPL _var] =$value; }                }        }         Public functionDisplay$TPL){                $this->assign ($this-Key,$this-val); Extract($this-tpl_vars); if(file_exists($TPL)){//The template exists on the load file. Include $tpl;                        Ob_start(); include $TPL; $contents=ob_get_contents(); Ob_end_clean(); return $contents; }Else{                        return false; }        }}$TPL=NewMytpl;$arr=Array(' a ' = ' aaaaaaa ', ' b ' + =Array(' a ' = ' 111111 ', ' b ' = ' 22222 ', ' c ' = ' = ' 3333 '), ' c ' = ' CCCCCCC ', ' d ' = ' dddddd ', ' e ' = ' eeeee ');$str= ' I am a string ';$TPL->assign (' arr ',$arr);$TPL->assign (' str ',$str);$message=$TPL->display (' tpl.html ');Echo $message;?>

The extract function is used to import variables from the array into the current symbol table, the key to do the variable, the value of the value!

The import template also uses the Include function, starting with include, where any variables that are available at that row are available in the called file. However, all functions and classes defined in the include file have global scope. When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode to resume at the end of the file. For this reason, any code in the destination file that needs to be executed as PHP code must be included in the valid PHP start and end tags. (So template parsing is required to compile the template). The way to "include" the Inlcude file in a variable is to use an output control function in conjunction with the include to capture its output.

If the template file does not change, then it does not need to be recompiled, so add the caching mechanism in the template engine to increase the speed of access.

Smarty, a template engine written in PHP, is one of the most famous PHP template engines in the industry, defining the smarty syntax and caching features that are very good for performance and usability.

You can also refer to this smart simple implementation http://www.cnblogs.com/isuhua/archive/2013/04/13/3019377.html

PHP Template principle

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.