第一次在这里帖码,此份代码主要是PHP模板引擎技术研究,目前只有编译版本,希望各位多多提供意见和优化技巧
三个文件组成,不知道如何以文件形式,只能复制了,抱歉!
index.php是一个配置文件,大伙看看就明白
index.html一些使用的例子
Templates.class.php基类 晚点发布下有缓存的完善版本,但希望没有在写缓存一些,有朋友或是高手指点下,这个模板引擎只要处理编译和缓存即可,其余考虑暂时不考虑,当然正则替换模式还要增加f,w之类。。。 希望有朋友可以研究研究本人Q:
76376931 Copy_3_of_Templates.class.php 文件是已经增加缓存方式的,再次刷新页面不会生成缓存,未考虑项目中某些页面是否要缓存,以后用该类在逐步添加,希望有朋友可以一起交流!
- 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/');
- //是否开启缓冲区
- define('NEW_CACHE', false);
- //判断是否开启缓冲区
- NEW_CACHE ? ob_start() : null;
- //引入模板类
- require ROOT_HOST.'/lib/Templates.class.php';
- $_moban = new Templates();
- $array = array(a=>'你好呀',b=>'我不是很好,但我很想你',c=>'你都在家里了,怎么还想我呀?');
- $xcvu = '你好啊,这是一个XCVU';
- $zmq = "hi";
- $title = "这是一个模板引擎自定义方法!";
- $ling = "因为正在修改一个“函数”????????????????";
- $_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');
- ?>
- BBBasd不知道说点什么好,可又想说点什么好
-
-
-
-
-
-
1号 -
-
2号 -
-
-
- ........
-
-
- /* 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(
- '/<\!--\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["$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 = '/<\!--\s+include\s+file=\"([\w\.\-]+)\"\s+\-->/';
- 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(
- '/<\!--\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["$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 = '/<\!--\s+include\s+file=\"([\w\.\-]+)\"\s+\-->/';
- 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);
- }
- }
-
- }
- ?>
|