Php simple template engine and php template engine
The PHP template engine is a PHP class library that separates PHP code from HTML code, significantly improving code readability and maintainability. The advantage of doing so is to let the artist concentrate on designing the HTML front-end page, and the programmer concentrate on writing the PHP business logic. Therefore, the ghost engine is suitable for the company's Web development team, so that everyone can take advantage of its expertise.
Next, let's take a look at how to implement the php template engine.
Parser. class. php
<? Php/*** template parsing class */class Parser {// field, receiving the template file content private $ _ tpl; // constructor, get the template file content public function _ construct ($ _ tplFile) {if (! $ This-> _ tpl = file_get_contents ($ _ tplFile) {exit ('error: Template File Read error'); }}// parse the common variable private function parvar () {$ _ patten = '/<! -- \ S + \ {\ $ ([\ w] +) \} \ s + -->/'; if (preg_match ($ _ patten, $ this-> _ tpl) {$ this-> _ tpl = preg_replace ($ _ patten, "<? Php echo \ $ this-> _ vars ['$ 1'];?> ", $ This-> _ tpl) ;}// parse the IF statement private function parif () {$ _ pattenif = '/<! -- \ S + \ {if \ s + \ $ ([\ w] +) \} \ s + -->/'; $ _ pattenElse ='/<! -- \ S + \ {else \} \ s + -->/'; $ _ pattenEndif ='/<! -- \ S + \ {\/if \} \ s + -->/'; if (preg_match ($ _ pattenif, $ this-> _ tpl )) {if (preg_match ($ _ pattenEndif, $ this-> _ tpl) {$ this-> _ tpl = preg_replace ($ _ pattenif, "<? Php if (\ $ this-> _ vars ['$ 1']) {?> ", $ This-> _ tpl); $ this-> _ tpl = preg_replace ($ _ pattenEndif," <? Php }?> ", $ This-> _ tpl); if (preg_match ($ _ pattenElse, $ this-> _ tpl) {$ this-> _ tpl = preg_replace ($ _ pattenElse, "<? Php} else {?> ", $ This-> _ tpl) ;}} else {echo 'error: The IF statement is not closed! ';}}// PHP annotation parsing private function parCommon () {$ _ pattenCommon ='/<! -- \ S + \{#\}(. *) \{#\}\ s + -->/'; if (preg_match ($ _ pattenCommon, $ this-> _ tpl )) {$ this-> _ tpl = preg_replace ($ _ pattenCommon, "<? Php/* $1 */?> ", $ This-> _ tpl) ;}// parse the foreach statement private function parForeach () {$ _ pattenForeach = '/<! -- \ S + \ {foreach \ s + \ $ ([\ w] +) \ ([\ w] +), ([\ w] + )\) \}\ s + -->/'; $ _ pattenForeachEnd ='/<! -- \ S + \ {\/foreach \} \ s + -->/'; $ _ pattenForeachValue ='/<! -- \ S + \ {@ ([\ w] +) \} \ s + -->/'; if (preg_match ($ _ pattenForeach, $ this-> _ tpl )) {if (preg_match ($ _ pattenForeachEnd, $ this-> _ tpl) {$ this-> _ tpl = preg_replace ($ _ pattenForeach, "<? Php foreach (\ $ this-> _ vars ['$ 1'] as \ $2 =>\$ $3) {?> ", $ This-> _ tpl); $ this-> _ tpl = preg_replace ($ _ pattenForeachEnd," <? Php }?> ", $ This-> _ tpl); if (preg_match ($ _ pattenForeachValue, $ this-> _ tpl) {$ this-> _ tpl = preg_replace ($ _ pattenForeachValue, "<? Php echo \ $1;?> ", $ This-> _ tpl) ;}} else {echo 'error: The Foreach statement is not closed! ';}}// Parse the include method private function parInclude () {$ _ pattenInclude ='/<! -- \ S + \ {include \ s + file = \ "([\ w \. \-] +) \ "\} \ s + -->/'; if (preg_match ($ _ pattenInclude, $ this-> _ tpl, $ _ file, $ _ file) {if (! File_exists ($ _ file [1]) | empty ($ _ file) {echo 'error: file Inclusion ERROR! ';} $ This-> _ tpl = preg_replace ($ _ pattenInclude, "<? Php include '$ 1';?> ", $ This-> _ tpl) ;}// resolve the system variable method private function parConfig () {$ _ pattenConfig = '/<! -- \ S + \ {([\ w] +) \} \ s + -->/'; if (preg_match ($ _ pattenConfig, $ this-> _ tpl )) {$ this-> _ tpl = preg_replace ($ _ pattenConfig, "<? Php echo \ $ this-> _ config ['$ 1'];?> ", $ This-> _ tpl) ;}// public function compile ($ _ path) {// parse the Template File $ this-> parvar (); $ this-> parif (); $ this-> parForeach (); $ this-> parInclude (); $ this-> parCommon (); $ this-> parConfig (); // generate the compilation file if (! File_put_contents ($ _ path, $ this-> _ tpl) {exit ('error: An ERROR occurred while compiling the file! ') ;}}?>
Templates. class. php
<? Php/*** template class */class Templates {// injection variable private $ _ vars = array (); // save the system variable array FIELD private $ _ config = array (); // create a constructor to check whether public function _ construct () {if (! Is_dir (TPL_DIR) |! Is_dir (TPL_C_DIR) |! Is_dir (CACHE) |! Is_dir (CONFIG) {echo 'error: Template directory or compilation directory. The cache directory does not exist! Auto create! '. "<Br/>"; if (! Is_dir (TPL_DIR) {mkdir (TPL_DIR); echo 'template directory '. TPL_DIR.' '. "<br/>";} if (! Is_dir (TPL_C_DIR) {mkdir (TPL_C_DIR); echo 'compile directory '. TPL_C_DIR.' '. "<br/>" ;}if (! Is_dir (CACHE) {mkdir (CACHE); echo 'cache directory '. CACHE.' '. "<br/>" ;}if (! Is_dir (CONFIG) {mkdir (CONFIG); echo 'cache directory '. CONFIG. 'create '. "<br/>" ;}exit () ;}// save the system variable $ _ sxe = simplexml_load_file (CONFIG. '/config. xml '); $ _ tagLib =$ _ sxe-> xpath ('/root/taglib '); foreach ($ _ tagLib as $ _ tag) {$ this-> _ config ["$ _ tag-> name"] =$ _ tag-> value ;}// assign () method, used to inject the public function assign ($ _ var, $ _ value) variable) {// $ _ var is used to synchronize the variable name in the template. // $ _ value indicates the value if (isset ($ _ var )&&! Empty ($ _ var) {$ this-> _ vars [$ _ var] = $ _ value;} else {exit ('error: Set the template variable! ') ;}} // Display () method public function display ($ _ file) {$ _ tplFile = TPL_DIR. $ _ file; // determine whether the object exists if (! File_exists ($ _ tplFile) {echo 'error: The template file does not exist! The Index. tpl template file is automatically created! '; File_put_contents ($ _ tplFile, 'index'); exit () ;}// generate the compilation file $ _ path = TPL_C_DIR.md5 ($ _ file ). '-'. $ _ file. '. php '; // cache file $ _ cacheFile = cache.md5(1__file).'-'.20._file.'html'; // when the same file is run for the second time, it is directly loaded into the cache file if (IS_CACHE) {// determine if (file_exists ($ _ cacheFile) & file_exists ($ _ path )) {// determine whether the template file has been modified if (filemtime ($ _ path)> = filemtime ($ _ tplFile) & filemtime ($ _ cacheFile)> = filemtime ($ _ path) {include $ _ cacheFile; Echo '<! -- Cache --> '; return ;}}// if the compiled file does not exist or the file changes, the if (! File_exists ($ _ path) | filemtime ($ _ path) <filemtime ($ _ tplFile) {require ROOT_PATH. '/Class/parser. class. php '; // The constructor is to input the template file address $ _ parser = new Parser ($ _ tplFile ); // input the compilation file address $ _ parser-> compile ($ _ path);} // load the compilation file include $ _ path; if (IS_CACHE) {// obtain the buffer data file_put_contents ($ _ cacheFile, ob_get_contents (); // clear the buffer ob_end_clean (); // load the cache file include $ _ cacheFile; }}}?>
Templates. php
<? Php // set the character encoding 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', ROOT_PATH. '/Cache/'); // system variable configuration directory define ('config', ROOT_PATH. '/Config/'); // whether to enable the buffer define ('is _ cache', false); // false // determine whether to enable IS_CACHE? Ob_start (): null; // introduces the template class require ROOT_PATH. '/Class/Templates. class. php '; // instantiate the template class $ _ tpl = new Templates (); $ _ tpl-> display ('index. tpl ');?>
Templates/index. tpl
<! DOCTYPE html>
Config/config. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Root> <taglib> <name> WebName </name> <value> XXX website </value> </taglib> </root>
Articles you may be interested in:
- Cache application in php smarty template engine
- Cache application in php smarty template engine
- Variable operators and usage in php smarty template engine
- Variable output of ThinkPHP template engine