# Phpchina debut #
Smarty has always been regarded as a superfluous thing. I think there is nothing more than Smarty. Today, I will teach you how to write a template engine so that you can write your own template engine. After reading this article, you will learn more about Smarty. My template engine name is Stupid ("dumb"). I don't like things that are so clever!
The Stupid template engine consists of three files: stupid. class. php, stupid_parser.class.php, and stupid_debugger.class.php.
Stupid. class. php is used to set variables, template paths, and display functions. stupid_parser.class.php is used to compile the template file and stupid_debugger.class.php is used for debugging.
Now let's write stupid. class. php first.
1. Create a php file named stupid. class. php.
Our class is Stupid. Let's design the member variables first.
Member variables include: $ _ tpl_vars, $ _ tpl_file, $ _ parser, $ _ debugger;
$ _ Tpl_vars: used to save template variables;
$ _ Tpl_file: used to save the template file name;
$ _ Parser: The compiled object that saves the StupidParser object;
$ _ Debugger: the debugging object that saves the StupidDebug object;
The following two constants are defined to store the template folder and the compiled Folder:
Define (TPL_DIR,./templates /);
Define (TPL_C_DIR,./templates_c /);
Start encoding >>>
<? Php
Define (TPL_DIR,./templates /);
Define (TPL_C_DIR,./templates_c /);
Class Stupid {
Private $ _ tpl_vars;
Private $ _ tpl_file;
Private $ _ parser;
Private $ _ debugger;
}
?>
Start to write a constructor>
Public function Stupid (){
If (! Is_dir (TPL_DIR) |! Is_dir (TPL_C_DIR )){
Exit (error: Set the template folder and compile folder correctly );
}
}
In the constructor, we determine whether the template path and compilation path are set correctly.
Design our method
We mainly use the following methods in this class:
Assign (), set_tpl_dir (), set_parsed_dir (), display (), debug ().
Assign () method:
The use of assign () is to set template variables. The Code is as follows>
Public function assign ($ var, $ value ){
If (isset ($ var) & trim ($ var )! = ){
$ This-> _ tpl_vars [$ var] = $ value;
Return true;
} Else {
Exit (error: Please set the variable name );
}
}
First, determine whether the variable name is set. Use isset ($ var) & trim ($ var )! = To judge, trim ($ var )! = Is to prevent the user from setting the variable name with spaces. If the variable is set correctly, we will save it to the member Variable _ tpl_vars.
Display () method
The display () method is the most important method in the Stupid class. It is used to display and detect whether the template has been updated. After the update, it is compiled again. If there is no update, the original compiled file is used.
The Code is as follows >>>
Public function display ($ tpl_file ){
$ Template_file = TPL_DIR. $ tpl_file;
If (! File_exists ($ template_file )){
Exit (error: the template file does not exist );
}
$ Parsed_file = TPL_C_DIR.md5 ($ tpl_file).. php;
If (! File_exists ($ parsed_file) | filemtime ($ parsed_file) <filemtime ($ template_file )){
Require_once./stupid_parser.class.php;
$ This-> _ parser = new StupidParser ();
$ This-> _ parser-> compile ($ tpl_file );
}
Include $ parsed_file;
}
This method is based on! File_exists ($ parsed_file) | filemtime ($ parsed_file) <filemtime ($ template_file) statement to determine whether the template file has been compiled and updated, the template files that have not been compiled or updated must be re-compiled. we will introduce stupid_parser.class.php, create the StupidParser object, and compile the template file. after compilation, we will introduce the compiled files. the compiled template file is a common PHP file.
Debug () method
The Debugg () method is relatively simple, that is, the stupid_debugger.class.php file is introduced, the StupidDebuger object is created, and the start method of StupidDebuger is called for debugging.
The Code is as follows >>>
Public function debug ($ tpl_file ){
If (include_once ("stupid_debugger.class.php ")){
$ This-> _ debugger = new StupidDebugger (TPL_DIR. $ tpl_file );
$ This-> _ debugger-> start ();
} Else {
Exit (error: the Debuger class file does not exist );
}
}
So far, our Stupid class is finished! Next time I will introduce the StupidParser class writing. Please continue to support it. You may have any comments or suggestions!
Show full phase:
<? Php
Define (TPL_DIR,./templates /);
Define (TPL_C_DIR,./templates_c /);
Class Stupid {
Private $ _ tpl_vars;
Private $ _ tpl_file;
Private $ _ parser;
Private $ _ debug;
Public function Stupid (){
If (! Is_dir (TPL_DIR) |! Is_dir (TPL_C_DIR )){
Exit (error: Set the template folder and compile folder correctly );
}
}
Public function assign ($ var, $ value ){
If (isset ($ var) & trim ($ var )! = ){
$ This-> _ tpl_vars [$ var] = $ value;
Return true;
} Else {
Exit (error: Please set the variable name );
}
}
Public function display ($ tpl_file ){
$ Template_file = TPL_DIR. $ tpl_file;
If (! File_exists ($ template_file )){
Exit (error: the template file does not exist );
}
$ Parsed_file = TPL_C_DIR.md5 ($ tpl_file).. php;
If (! File_exists ($ parsed_file) | filemtime ($ parsed_file) <filemtime ($ template_file )){
Require_once./stupid_parser.class.php;
$ This-> _ parser = new StupidParser ();
$ This-> _ parser-> compile ($ tpl_file );
}
Include $ parsed_file;
}
Function debug ($ tpl_file ){
If (include_once ("stupid_debugger.class.php ")){
$ This-> _ debugger = new StupidDebugger ($ this-> _ template_dir. $ tpl_file );
$ This-> _ debugger-> start ();
} Else {
Exit (error: the Debuger class file does not exist );
}
}
}
?>