Home >Backend Development >PHP Tutorial >A simple template engine class. This type of research is not perfect. I hope friends can participate in the study and research.

A simple template engine class. This type of research is not perfect. I hope friends can participate in the study and research.

WBOY
WBOYOriginal
2016-07-25 09:01:19984browse
This is the first time I post code here. This code is mainly for PHP template engine technology research. Currently there is only a compiled version. I hope you can provide more opinions and optimization tips.
It consists of three files. I don’t know how to format it as a file, so I can only copy it. Sorry!
index.php is a configuration file, everyone will understand after taking a look
index.html Some usage examples
Templates.class.php base class
A perfect version of caching will be released later, but I hope it will not be cached. With the guidance of friends or experts, this template engine only needs to handle compilation and caching, and other considerations will not be considered for the time being. , of course, the regular replacement mode also needs to add f, w and so on. . .
I hope some friends can study my Q:
76376931

Copy_3_of_Templates.class.php file has added a cache method. Refreshing the page again will not generate a cache. It has not been considered whether some pages in the project need to be cached. This class will be added gradually in the future. I hope some friends can join me. comminicate!
  1. header('Content-Type:text/html;charset=utf-8');
  2. define('ROOT_HOST',dirname(__FILE__));
  3. define('HTML_DIR',ROOT_HOST. '/moban/');
  4. define('COMPILED_DIR',ROOT_HOST.'/data/compiled/');
  5. define('CACHE_DIR',ROOT_HOST.'/data/cache/');
  6. //Whether to enable the buffer
  7. define('NEW_CACHE', false);
  8. //Determine whether to open the buffer
  9. NEW_CACHE? ob_start(): null;
  10. //Introduce the template class
  11. require ROOT_HOST.'/lib/Templates.class.php';
  12. $_moban = new Templates();
  13. $array = array(a=>'Hello',b=>'I'm not very good, but I miss you very much',c=>'You are all here You're at home, why do you still miss me?');
  14. $xcvu = 'Hello, this is an XCVU';
  15. $zmq = "hi";
  16. $title = "This is a template engine customization method!" ;
  17. $ling = "Because a "function" is being modified????????????????";
  18. $_moban->assign('ling', $ling);
  19. $ _moban->assign('title',$title);
  20. $_moban->assign('zmq', $zmq);
  21. $_moban->assign('xcvu', $xcvu);
  22. $_moban- >assign('abc',5>4);
  23. $_moban->assign('array', $array);
  24. $_moban->display('index.html');
  25. ?>
Copy code
  1. <!-- $title -->< ;/title></li> <li><style type="text/css"></li> <li>*{ margin:0; padding:0;}</li> <li>body{ font-size:12px; color:#fff; background:#999;}</li> <li> .index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}</li> <li>.index a{color:#000; text-decoration :none;}</li> <li>.index a:hover{ color:#F0F;}</li> <li></style></li> <li></head></li> <li><body></li> <li><div class="index"></li> <li> <span style ="color:#000;">BBBasd</span><span style="color:#000;">I don’t know what to say, but I want to say something</span>< br /></li> <li> <a href="#"><!-- $ling --></a></li> <li> <br /></li> <li> <!-- $xcvu --> </li> <li> <br /></li> <li> <!-- if $abc --></li> <li> <P>No.1</P></li> <li> <!-- else --></li> <li> <P>No.2 </P></li> <li> <!-- /if --></li> <li> <br /></li> <li> <!-- loop $array(k,v) --></li> <li> <!-- @k -->........<!-- @v --><br/></li> <li> <!-- /loop --></li> <li> <!-- #This is PHP comments --></li> <li></div></li> <li></body></li> <li></html></li> </ol></div> <em onclick="copycode($('code_NP9'));">Copy code</em> </div> <div class="blockcode"> <div id="code_zuF"><ol> <li><?php<li>/* about:Richard.z<li> * site:http://www.zmq.cc<li> * E_mail:code@zmq.cc<li> * date:2013/01/02/17:30<li> * */<li>class Templates{<li> private $_CaChe;<li> private $_Compiled;<li> private $_HtmlFile;<li> private $_FileVar;<li> private $_KeyArr = array();<li> <li> public function __construct(){<li> if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){<li> exit('Your directory does not exist!');<li> }<li> }<li> <li> public function assign($_var, $_value){<li> if(isset($_var) && !empty($_var)){<li> $this->_KeyArr[$_var] = $_value;</li> <li> }else{</li> <li> exit('Please set your value!');</li> <li> } </li> <li> }</li> <li> </li> <li> public function display($_File){</li> <li> //设置模板的变量</li> <li> $this->_HtmlFile = HTML_DIR.$_File;</li> <li> //设置编译</li> <li> $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';</li> <li> //设置缓存</li> <li> $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';</li> <li> //判断模板是否存在</li> <li> if(!file_exists($this->_HtmlFile)){</li> <li> exit('Template file does not exist');</li> <li> }</li> <li> //赋值和判断读取</li> <li> if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){</li> <li> exit('The template file read error!');</li> <li> }</li> <li> //if edit Compiled File date < date HtmlFile <li> if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){</li> <li> $this->Set_Comilled();</li> <li> }</li> <li> //Include Compiled</li> <li> include $this->_Compiled; </li> <li> }</li> <li> <li> //public function</li> <li> public function Set_Comilled(){</li> <li> $this->SetArr();</li> <li> $this->SetInclude();</li> <li> if(!file_put_contents($this->_Compiled, $this->_FileVar)){</li> <li> exit('Compiled files generated error!');</li> <li> }</li> <li> }</li> <li> //arr</li> <li> private function SetArr(){</li> <li> $_preaa = array(</li> <li> '/<!--s+$([w]+)s+-->/',</li> <li> '/<!--s+ifs+$([w]+)s+-->/',</li> <li> '/<!--s+/ifs+-->/',</li> <li> '/<!--s+elses+-->/',</li> <li> '/<!--s+loops+$([w]+)(([w]+),([w]+))s+-->/',</li> <li> '/<!--s+/loops+-->/',</li> <li> '/<!--s+@([w]+)s+-->/',</li> <li> '/<!--s+#(.*)s+-->/');</li> <li> $_prebb = array(</li> <li> '<?php echo $this->_KeyArr["$1"];?>',</li> <li> '<?php if ($this->_KeyArr["$1"]) {?>',</li> <li> '<?php } ?>',</li> <li> '<?php } else { ?>',</li> <li> '<?php foreach ($this->_KeyArr["$1"] as $$2=>$$3) { ?>',</li> <li> '<?php } ?>',</li> <li> '<?php echo $$1; ?>',</li> <li> '<?php /* $1 */ ?>');</li> <li> $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);</li> <li> if(preg_match($_preaa[0], $this->_FileVar)){</li> <li> $this->_FileVar = $this->SetArr($this->_FileVar);</li> <li> }</li> <li> }</li> <li> <li> //Include</li> <li> private function SetInclude(){</li> <li> $_preFile = '/<!--s+includes+file="([w.-]+)"s+-->/';</li> <li> if(preg_match($_preFile, $this->_FileVar,$_File)){</li> <li> if(!file_exists($_File[1]) || empty($_File)){</li> <li> exit('You of Include File Error!');</li> <li> }</li> <li> $this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);</li> <li> }</li> <li> }</li> <li> </li> <li>}</li> <li>?></li> </ol></div> <em onclick="copycode($('code_zuF'));">复制代码</em> </div> <div class="blockcode"> <div id="code_pmx"><ol> <li><?php<li>/* about:Richard.z<li> * site:http://www.zmq.cc<li> * E_mail:code@zmq.cc<li> * date:2013/01/02/17:30 || 2013/01/14/21:35<li> * */<li>class Templates{<li> private $_CaChe;<li> private $_Compiled;<li> private $_HtmlFile;<li> private $_FileVar;<li> private $_KeyArr = array();<li> <li> public function __construct(){<li> if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){<li> exit('Your directory does not exist!');<li> }<li> }<li> <li> public function assign($_var, $_value){<li> if(isset($_var) && !empty($_var)){<li> $this->_KeyArr[$_var] = $_value;</li> <li> }else{</li> <li> exit('Please set your value!');</li> <li> } </li> <li> }</li> <li> </li> <li> public function display($_File){</li> <li> //设置模板的变量</li> <li> $this->_HtmlFile = HTML_DIR.$_File;</li> <li> //设置编译</li> <li> $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';</li> <li> //设置缓存</li> <li> $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';</li> <li> //判断模板是否存在</li> <li> if(!file_exists($this->_HtmlFile)){</li> <li> exit('Template file does not exist');</li> <li> }</li> <li> //赋值和判断读取</li> <li> if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){</li> <li> exit('The template file read error!');</li> <li> }</li> <li> //if edit Compiled File date < date HtmlFile <li> if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){</li> <li> $this->Set_Comilled();</li> <li> }</li> <li> //Include Compiled</li> <li> include $this->_Compiled;</li> <li> $this->SetCaChe();</li> <li> }</li> <li> <li> //The setting cache file if you want to be generated again</li> <li> private function SetCaChe(){</li> <li> if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) < filemtime($this->_Compiled)){</li> <li> if(NEW_CACHE){</li> <li> file_put_contents($this->_CaChe, ob_get_contents());</li> <li> ob_end_clean();</li> <li> include $this->_CaChe;</li> <li> }</li> <li> }</li> <li> }</li> <li> <li> //public function</li> <li> public function Set_Comilled(){</li> <li> $this->SetArr();</li> <li> $this->SetInclude();</li> <li> if(!file_put_contents($this->_Compiled, $this->_FileVar)){</li> <li> exit('Compiled files generated error!');</li> <li> }</li> <li> }</li> <li> //arr</li> <li> private function SetArr(){</li> <li> $_preaa = array(</li> <li> '/<!--s+$([w]+)s+-->/',</li> <li> '/<!--s+ifs+$([w]+)s+-->/',</li> <li> '/<!--s+/ifs+-->/',</li> <li> '/<!--s+elses+-->/',</li> <li> '/<!--s+loops+$([w]+)(([w]+),([w]+))s+-->/',</li> <li> '/<!--s+/loops+-->/',</li> <li> '/<!--s+@([w]+)s+-->/',</li> <li> '/<!--s+#(.*)s+-->/');</li> <li> $_prebb = array(</li> <li> '<?php echo $this->_KeyArr["$1"];?>',</li> <li> '<?php if ($this->_KeyArr["$1"]) {?>',</li> <li> '<?php } ?>',</li> <li> '<?php } else { ?>',</li> <li> '<?php foreach ($this->_KeyArr["$1"] as $$2=>$$3) { ?>',</li> <li> '<?php } ?>',</li> <li> '<?php echo $$1; ?>',</li> <li> '<?php /* $1 */ ?>');</li> <li> $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);</li> <li> if(preg_match($_preaa[0], $this->_FileVar)){</li> <li> $this->_FileVar = $this->SetArr($this->_FileVar);</li> <li> }</li> <li> }</li> <li> <li> //Include</li> <li> private function SetInclude(){</li> <li> $_preFile = '/<!--s+includes+file="([w.-]+)"s+-->/';</li> <li> if(preg_match($_preFile, $this->_FileVar,$_File)){</li> <li> if(!file_exists($_File[1]) || empty($_File)){</li> <li> exit('You of Include File Error!');</li> <li> }</li> <li> $this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);</li> <li> }</li> <li> }</li> <li> </li> <li>}</li> <li>?></li> </ol></div> <em onclick="copycode($('code_pmx'));">复制代码</em> </div> </td></tr></table> <div id="comment_35146" class="cm"> </div> <div id="post_rate_div_35146"></div> <br><br></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="Three-level linkage of provinces, cities and counties with mysql+ajax" href="https://m.php.cn/faq/317102.html">Three-level linkage of provinces, cities and counties with mysql+ajax</a></span><span>Next article:<a class="dBlack" title="Three-level linkage of provinces, cities and counties with mysql+ajax" href="https://m.php.cn/faq/317111.html">Three-level linkage of provinces, cities and counties with mysql+ajax</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>