PHP makes simple template engine, PHP template engine
PHP template engine is a PHP class library, using it to make the PHP code and HTML code separation, so that the readability and maintenance of the code has been significantly improved. And the good thing is, let the artwork focus on the HTML foreground page, and the programmer is focused on writing the PHP business logic. Therefore, the modular engine is well suited to the company's web development team, so that everyone can play their strengths
Let's take a look at how to implement the PHP template engine in a simple way
parser.class.php
<?php/** * Template parsing class */class parser{//field, receive template file content private $_tpl; Construct method, get template file contents Public function __construct ($_tplfile) {if (! $this->_tpl = file_get_contents ($_tplfile)) { Exit (' ERROR: template file read Errors '); }}//parse ordinary variable private function Parvar () {$_patten = '/
/'; if (Preg_match ($_patten, $this->_tpl)) {$this->_tpl = preg_replace ($_patten, "<?php echo \ $this->_vars[' $ 1 '];? > ", $this->_tpl); }}//Resolve if statement private function Parif () {$_pattenif = '/
/'; $_pattenelse = '/
/'; $_pattenendif = '/
/'; if (Preg_match ($_pattenif, $this->_tpl)) {if (Preg_match ($_pattenendif, $this->_tpl)) {$this->_TPL = Preg_replace ($_pattenif, "<?php if (\ $this->_vars[']) {?>", $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:if statement is not closed! '; }}}//php comment parsing private function Parcommon () {$_pattencommon = '/
/'; if (Preg_match ($_pattencommon, $this->_tpl)) {$this->_tpl = preg_replace ($_pattencommon, "<?php/* $ * *) ; ", $this->_TPL); }}//Parse foreach statement private function Parforeach () {$_pattenforeach = '/
/'; $_pattenforeachend = '/
/'; $_pattenforeachvalue = '/
/'; if (Preg_match ($_pattenforeach, $this->_tpl)) {if (Preg_match ($_pattenforeachend, $this->_tpl)) {$this-& GT;_TPL = Preg_replace ($_pattenforeach, "<?php foreach (\ $this->_vars['] 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:foreach statement is not closed! '; }}}//parsing the Include method private function Parinclude () {$_patteninclude = '/
/'; if (Preg_match ($_patteninclude, $this->_tpl,$_file,$_file)) {if (!file_exists ($_file[1)) | | Empty ($_file)) {echo ' ERROR: Error containing file! '; } $this->_tpl = Preg_replace ($_patteninclude, "<?php include ' $ ';?") > ", $this->_tpl); }}//Parse system variable method private function Parconfig () {$_pattenconfig = '/
/'; if (Preg_match ($_pattenconfig, $this->_tpl)) {$this->_tpl = preg_replace ($_pattenconfig, "<?php echo \ $this-& gt;_config[' $ '];? > ", $this->_tpl); }}//External public method common function compile ($_path) {//parse template file $this->parvar (); $this->parif (); $this->parforeach (); $this->parinclude (); $this->parcommon (); $this->parconfig (); Generate the Compile file if (! file_put_contents ($_path, $this->_tpl)) {exit (' ERROR: Compile file build Error! '); }}}?>
Templates.class.php
<?php/** * Template class */class templates{//injection variable private $_vars = Array (); Save system variable Array field private $_config = Array (); Create a constructor method to detect if the directory exists public function __construct () {if (! Is_dir (tpl_dir) | |! Is_dir (TPL_C_DIR) | |! Is_dir (CACHE ) || !is_dir (CONFIG)) {echo ' ERROR: Template directory or compiled directory, cache directory does not exist! Automatically created! '."
"; if (!is_dir (Tpl_dir)) {mkdir (tpl_dir); echo ' template catalog '. Tpl_dir. ' Building '.
"; } if (!is_dir (Tpl_c_dir)) {mkdir (tpl_c_dir); echo ' Compile directory '. Tpl_c_dir. ' Building '.
"; } if (!is_dir (cache)) {mkdir (cache); echo ' Cache directory '. CACHE. ' Build '.
"; } if (!is_dir (config)) {mkdir (config); echo ' Cache directory '. CONFIG. ' Build '.
"; } exit (); }//Save 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 variable public function assign ($_var,$_value) {//$_var for the variable name in the synchronization template//$_value represents the value if (Isset ($_va R) &&!empty ($_var)) {$this->_vars[$_var] = $_value; }else{exit (' ERROR: set template variable! '); }}//display () method public function display ($_file) {$_tplfile = Tpl_dir. $_file; Determine if the file exists if (! file_exists ($_tplfile)) {echo ' ERROR: Template file does not exist! Create Index.tpl template files automatically! '; File_put_contents ($_tplfile, ' Index '); Exit (); }//Generate compiled file $_path = tpl_c_dir.md5 ($_file). ' -'. $_file. ' PHP '; Cache file $_cachefile = CACHE.MD5 ($_file). ' -'. $_file. ' HTML '; When you run the same file for the second time, load the cache file directly if (Is_cache) {///determine the cache file and the compiled file are present if (file_exists ($_cachefile) &&file_exists ($_PA TH)) {//Determine if the template file has been modified if (Filemtime ($_path) >=filemtime ($_tplfile) &&filemtime ($_cachefile) >=filemtime ($_path)) {in Clude $_cachefile; Echo '
'; Return }}}//Regenerate if the compiled file does not exist or if the file has changed (!file_exists ($_path) | | | Filemtime ($_path)
compile ($_path); }//Load compilation file include $_path; if (Is_cache) {//Get buffer Data file_put_contents ($_cachefile,ob_get_contents ()); Clear buffer Ob_end_clean (); Loading cache files include $_cachefile; }}}?>
templates.php
<?php//Set character encoding Utf-8header (' content-type:text/html;charset=utf-8 '); Website root directory define (' Root_path ', DirName (__file__)); Store template folder define (' Tpl_dir ', Root_path. ' /templates/'); Compile 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 open buffer define (' Is_cache ', false);//false//Determine if Is_cache is required? Ob_start (): null; Introduce template class require Root_path. ' /class/templates.class.php '; Instantiate template class $_tpl=new Templates (); $_tpl->display (' Index.tpl ');? >
Templates/index.tpl
<!--{WebName}--
123
321
...
System variables
Common variables
Config/config.xml
<?xml version= "1.0" encoding= "UTF-8"?> WebName
XXX website
Articles you may be interested in:
- Caching applications in the PHP smarty template engine
- Caching applications in the PHP smarty template engine
- PHP smarty template engine variable operators and how to use them
- thinkphp template engine Variable output detailed
http://www.bkjia.com/PHPjc/1119999.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119999.html techarticle PHP makes a simple template engine, PHP template engine php templates engine is a PHP class library, using it can be used to separate the PHP code and HTML code, so that the readability and maintenance of the code to be obvious ...