Caterpillar teaches you to write a template engine of your own.

Source: Internet
Author: User
The Caterpillar teaches you to write a template engine of your own, read the Caterpillar and teach you to write a template engine of your own, # phpchina debut # Smarty has always been regarded as a superfluous thing, I think the excess talents of Smarty are superfluous .... don't say that. Today, I will teach you how to write a template engine so that you can write your own template engine, and "> <LINKhref =" http: // www.

# 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 >>>

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: 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:

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: 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 ');
}
}
}
?>

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.