The first time here, this code is mainly PHP template engine technology research, currently only compiled version, I hope you will provide advice and optimization skills three file composition, do not know how to file form, can only copy, sorry! index.php is a configuration file, folks, just look at it. index.html Some examples of use Templates.class.php base class Late release has a perfect version of the cache, but I hope not in the write cache some, a friend or expert guidance, the template engine as long as the processing of compilation and caching, the rest of the consideration is not considered, of course, the regular replacement mode to increase the f,w and the like ... Hope to have a friend can study my Q: 76376931
Copy_3_of_Templates.class.php file is already increased cache mode, refresh the page again does not generate the cache, regardless of whether some pages in the project to cache, later with the class in the gradual addition, I hope friends can communicate together!
- Header (' Content-type:text/html;charset=utf-8 ');
- Define (' Root_host ', DirName (__file__));
- Define (' Html_dir ', root_host. /moban/');
- Define (' Compiled_dir ', root_host. /data/compiled/');
- Define (' Cache_dir ', root_host. /data/cache/');
- Whether to open buffer
- Define (' New_cache ', false);
- Determine if the buffer is turned on
- New_cache? Ob_start (): null;
- Introducing Template Classes
- Require Root_host. ' /lib/templates.class.php ';
- $_moban = new Templates ();
- $array = Array (a=> ' hello ',b=> ' I am not very good, but I miss you ',c=> ' you are at home, why still think of me? ');
- $XCVU = ' Hello, this is a XCVU ';
- $ZMQ = "HI";
- $title = "This is a template engine custom Method!";
- $ling = "because a function"???????????????? "is being modified;
- $_moban->assign (' Ling ', $ling);
- $_moban->assign (' title ', $title);
- $_moban->assign (' Zmq ', $ZMQ);
- $_moban->assign (' Xcvu ', $XCVU);
- $_moban->assign (' abc ', 5>4);
- $_moban->assign (' array ', $array);
- $_moban->display (' index.html ');
- ?>
Copy Code
-
-
-
-
- <!--$title-->
-
-
-
-
- BBBASD I don't know what to say, but I want to say something.
- !--$ling-->
-
- !--$xcvu-->
-
li>!--if $abc-->
-
1th
- !--Else-->
-
2nd
- !--/if-->
-
- !--loop $array (k,v)-->
- !--@k--> .... !--@v-->
- !--/loop-->
- !--#这个是PHP注释-->
-
-
-
Copy Code
- /* ABOUT:RICHARD.Z
- * site:http://www.zmq.cc
- * e_mail:code@zmq.cc
- * DATE:2013/01/02/17:30
- * */
- Class templates{
- Private $_cache;
- Private $_compiled;
- Private $_htmlfile;
- Private $_filevar;
- Private $_keyarr = Array ();
- Public Function __construct () {
- if (!is_dir (html_dir) | |!is_dir (COMPILED_DIR) | |!is_dir (CACHE_DIR)) {
- Exit (' Your directory does not exist! ');
- }
- }
- Public function assign ($_var, $_value) {
- if (Isset ($_var) &&!empty ($_var)) {
- $this->_keyarr[$_var] = $_value;
- }else{
- Exit (' Please set your value! ');
- }
- }
- Public function display ($_file) {
- Set a variable for a template
- $this->_htmlfile = html_dir.$_file;
- Set compilation
- $this->_compiled = COMPILED_DIR.MD5 ($_file). $_file. PHP ';
- Setting up the cache
- $this->_cache = CACHE_DIR.MD5 ($_file). $_file. HTML ';
- Determine if a template exists
- if (!file_exists ($this->_htmlfile)) {
- Exit (' Template file does not exist ');
- }
- Assignment and judgment reading
- if (! $this->_filevar = file_get_contents ($this->_htmlfile)) {
- Exit (' The template file read error! ');
- }
- If edit Compiled File Date < date Htmlfile
- if (!file_exists ($this->_compiled) | | filemtime ($this->_compiled) < Filemtime ($this->_htmlfile)) {
- $this->set_comilled ();
- }
- Include Compiled
- Include $this->_compiled;
- }
- Public function
- Public Function set_comilled () {
- $this->setarr ();
- $this->setinclude ();
- if (!file_put_contents ($this->_compiled, $this->_filevar)) {
- Exit (' Compiled files generated error! ');
- }
- }
- Arr
- Private Function Setarr () {
- $_PREAA = Array (
- '/<\!--\s+\$ ([\w]+) \s+\-->/',
- '/<\!--\s+if\s+\$ ([\w]+) \s+\-->/',
- '/<\!--\s+\/if\s+\-->/',
- '/<\!--\s+else\s+\-->/',
- '/<\!--\s+loop\s+\$ ([\w]+) \ (([\w]+), ([\w]+] \) \s+\-->/',
- '/<\!--\s+\/loop\s+\-->/',
- '/<\!--\s+@ ([\w]+) \s+\-->/',
- '/<\!--\s+\# (. *) \s+\-->/');
- $_prebb = Array (
- ' _keyarr[' "$"];? > ',
- ' _keyarr[' "]) {?> ',
- ' ',
- ' ',
- ' _keyarr[' ("$" ] as \$$2=>\$$3) {?> ',
- ' ',
- ' ',
- ' ');
- $this->_filevar = preg_replace ($_PREAA, $_prebb, $this->_filevar);
- if (Preg_match ($_preaa[0], $this->_filevar)) {
- $this->_filevar = $this->setarr ($this->_filevar);
- }
- }
- Include
- Private Function Setinclude () {
- $_prefile = '/<\!--\s+include\s+file=\ ' ([\w\.\-]+) \ ' \s+\-->/';
- if (Preg_match ($_prefile, $this->_filevar,$_file)) {
- if (!file_exists ($_file[1)) | | empty ($_file)) {
- Exit (' You Include File error! ');
- }
- $this->_filevar = preg_replace ($_prefile, " ", $this->_filevar);
- }
- }
- }
- ?>
Copy Code
- /* ABOUT:RICHARD.Z
- * site:http://www.zmq.cc
- * e_mail:code@zmq.cc
- * date:2013/01/02/17:30 | | 2013/01/14/21:35
- * */
- Class templates{
- Private $_cache;
- Private $_compiled;
- Private $_htmlfile;
- Private $_filevar;
- Private $_keyarr = Array ();
- Public Function __construct () {
- if (!is_dir (html_dir) | |!is_dir (COMPILED_DIR) | |!is_dir (CACHE_DIR)) {
- Exit (' Your directory does not exist! ');
- }
- }
- Public function assign ($_var, $_value) {
- if (Isset ($_var) &&!empty ($_var)) {
- $this->_keyarr[$_var] = $_value;
- }else{
- Exit (' Please set your value! ');
- }
- }
- Public function display ($_file) {
- Set a variable for a template
- $this->_htmlfile = html_dir.$_file;
- Set compilation
- $this->_compiled = COMPILED_DIR.MD5 ($_file). $_file. PHP ';
- Setting up the cache
- $this->_cache = CACHE_DIR.MD5 ($_file). $_file. HTML ';
- Determine if a template exists
- if (!file_exists ($this->_htmlfile)) {
- Exit (' Template file does not exist ');
- }
- Assignment and judgment reading
- if (! $this->_filevar = file_get_contents ($this->_htmlfile)) {
- Exit (' The template file read error! ');
- }
- If edit Compiled File Date < date Htmlfile
- if (!file_exists ($this->_compiled) | | filemtime ($this->_compiled) < Filemtime ($this->_htmlfile)) {
- $this->set_comilled ();
- }
- Include Compiled
- Include $this->_compiled;
- $this->setcache ();
- }
- The setting cache file if you want to be generated again
- Private Function Setcache () {
- if (!file_exists ($this->_cache) | | filemtime ($this->_cache) < Filemtime ($this->_compiled)) {
- if (New_cache) {
- File_put_contents ($this->_cache, ob_get_contents ());
- Ob_end_clean ();
- Include $this->_cache;
- }
- }
- }
- Public function
- Public Function set_comilled () {
- $this->setarr ();
- $this->setinclude ();
- if (!file_put_contents ($this->_compiled, $this->_filevar)) {
- Exit (' Compiled files generated error! ');
- }
- }
- Arr
- Private Function Setarr () {
- $_PREAA = Array (
- '/<\!--\s+\$ ([\w]+) \s+\-->/',
- '/<\!--\s+if\s+\$ ([\w]+) \s+\-->/',
- '/<\!--\s+\/if\s+\-->/',
- '/<\!--\s+else\s+\-->/',
- '/<\!--\s+loop\s+\$ ([\w]+) \ (([\w]+), ([\w]+] \) \s+\-->/',
- '/<\!--\s+\/loop\s+\-->/',
- '/<\!--\s+@ ([\w]+) \s+\-->/',
- '/<\!--\s+\# (. *) \s+\-->/');
- $_prebb = Array (
- ' _keyarr[' "$"];? > ',
- ' _keyarr[' "]) {?> ',
- ' ',
- ' ',
- ' _keyarr[' ("$" ] as \$$2=>\$$3) {?> ',
- ' ',
- ' ',
- ' ');
- $this->_filevar = preg_replace ($_PREAA, $_prebb, $this->_filevar);
- if (Preg_match ($_preaa[0], $this->_filevar)) {
- $this->_filevar = $this->setarr ($this->_filevar);
- }
- }
- Include
- Private Function Setinclude () {
- $_prefile = '/<\!--\s+include\s+file=\ ' ([\w\.\-]+) \ ' \s+\-->/';
- if (Preg_match ($_prefile, $this->_filevar,$_file)) {
- if (!file_exists ($_file[1)) | | empty ($_file)) {
- Exit (' You Include File error! ');
- }
- $this->_filevar = preg_replace ($_prefile, " ", $this->_filevar);
- }
- }
- }
- ?>
Copy Code |