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!
- 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 enable the buffer
- define('NEW_CACHE', false);
- //Determine whether to open the buffer
- NEW_CACHE? ob_start(): null;
- //Introduce the template class
- require ROOT_HOST.'/lib/Templates.class.php';
- $_moban = new Templates();
- $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?');
- $xcvu = 'Hello, this is an XCVU';
- $zmq = "hi";
- $title = "This is a template engine customization 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>
- *{ margin:0; padding:0;}
- body{ font-size:12px; color:#fff; background:#999;}
- .index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}
- .index a{color:#000; text-decoration :none;}
- .index a:hover{ color:#F0F;}
- BBBasdI don’t know what to say, but I want to say something< br />
-
-
-
-
-
-
No.1
-
-
No.2
-
-
-
- ........
-
-
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){
- //设置模板的变量
- $this->_HtmlFile = HTML_DIR.$_File;
- //设置编译
- $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
- //设置缓存
- $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
- //判断模板是否存在
- if(!file_exists($this->_HtmlFile)){
- exit('Template file does not exist');
- }
- //赋值和判断读取
- 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(
- '//',
- '//',
- '//',
- '//',
- '//',
- '//',
- '//',
- '//');
- $_prebb = array(
- '_KeyArr["$1"];?>',
- '_KeyArr["$1"]) {?>',
- '',
- '',
- '_KeyArr["$1"] 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 = '//';
- if(preg_match($_preFile, $this->_FileVar,$_File)){
- if(!file_exists($_File[1]) || empty($_File)){
- exit('You of Include File Error!');
- }
- $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
- }
- }
-
- }
- ?>
复制代码
- /* 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){
- //设置模板的变量
- $this->_HtmlFile = HTML_DIR.$_File;
- //设置编译
- $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
- //设置缓存
- $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
- //判断模板是否存在
- if(!file_exists($this->_HtmlFile)){
- exit('Template file does not exist');
- }
- //赋值和判断读取
- 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(
- '//',
- '//',
- '//',
- '//',
- '//',
- '//',
- '//',
- '//');
- $_prebb = array(
- '_KeyArr["$1"];?>',
- '_KeyArr["$1"]) {?>',
- '',
- '',
- '_KeyArr["$1"] 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 = '//';
- if(preg_match($_preFile, $this->_FileVar,$_File)){
- if(!file_exists($_File[1]) || empty($_File)){
- exit('You of Include File Error!');
- }
- $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
- }
- }
-
- }
- ?>
复制代码
|