电脑安全

您当前的位置:主页 > 电脑安全 >

php模板简单写法-网络编程

来源:[db:来源]标题:   文件    编辑:电脑技术全能网 时间:2019-09-20 18:31

原标题:php模板简单写法-网络编程
   这是一个简略的用php 完成的模板教程物。 class TEMPLATE { private $path = "." ; #根名目 private $var; private $tplDir = "template"; #模板存储名目 private $tplExt = "tpl"; #模板文件的后缀名 private $tplId = 0 ; #模板的ID号 private $compileDir = "template_c"; #编译后的php文件寄存名目 private $isCache=false ; #能否用缓存 (默许不启动) private $cacheId = 1; #缓存文件ID号 private $cacheLeftTime=3600; #缓存无效期 (默许保留3600秒) private $cacheDir = "cache"; #缓存文件存储名目 private $autoRefresh = false ; #能否主动革新 private $pattern = array( "/({dw:)s*includes*filename=s*"(.+..+)s*"s*(/})/i",#包括文件 "/({dw:)s*field.(.+)s*(/})/i",#部分变量 "/({dw:)s*global.(.+)s*(/})/i",#全局变量 "/({dw:)s*foreachs*(.+)s*ass*(.+)s*(/})/i",#foreach 语句 "/({dw:)s*ends*foreachs*(/})/i", #foreach 停止 "/({dw:)s*ifs*((.+))(/})/i", "/({dw:)s*elseifs*((.+))(/})/i", "/({dw:)s*elses*(/})/i", "/({dw:)s*ends*ifs*(/})/i", ); private $replacement = array( '<?php echo $this->inc_file("/2"); ?>', "<?php echo $/2;?>", "<?php global $/2;n echo $/2; ?>", "<?php foreach($/2 as $/3){ ?>", "<?php } ?>", "<?php if (/2) { ?>" , "<?php }else if(/2){ ?>", "<?php }else{ ?>", "<?php } ?>", ); #结构函数 function __construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="") { $this->path = $path ? $path : $this->path ; $this->tplDir = $tplDir ? $tplDir : $this->tplDir ; $this->compileDir = $compileDir ? $compileDir : $this->compileDir ; $this->isCache = is_bool($isCache) ? $isCache : $this->isCache ; $this->cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime ; $this->cacheDir = $cacheDir ? $cacheDir : $this->cacheDir ; $this->autoRefresh = is_bool($autoRefresh) ? $autoRefresh : $this->autoRefresh ; } #兼容php4 function TEMPLATE($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="") { $this->__construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh=""); } function __get($property) { return $this->$property ; } function __set($property,$value) { return $this->$property = $value ; } #给模板中的变量赋值 # $tplVal 模板中的变量名 function assign($tplVal ,$value="") { if (is_array($tplVal)) { foreach ($tplVal as $key => $val) { if (!empty($key)) $this->var[$key] = $val ; } } else { if (!empty($tplVal)) $this->var[$tplVal] = $value ; } } #输入文件内容函数 function display($tplFile,$tplId=0,$cacheId = 1,$cacheLeftTime="") { if (empty($tplFile)) die("Template "{$tplFile}" not exist !"); $this->cacheId = $cacheId ? md5($cacheId) : md5($this->cacheId); $cacheFile = $this->path. "/".$this->cacheDir."/".$tplFile.$this->cacheId ; if ($this->check_cache($cacheFile,$cacheLeftTime)) #当缓存文件存在且不外期时间接从缓存文件读取内容 { echo $this->read_file($cacheFile); }else { $tpl = $this->path."/".$this->tplDir."/".$tplFile.".".$this->tplExt; $tplContent = $this->read_file($tpl); #读取模板文件的内容 $compileContent= $this->compile_file($tplContent); #对读掏出来的文件停止编译 $this->tplId = $tplId ? $tplId : $this->tplId ; $compileFile = $this->path."/".$this->compileDir."/".md5($this->tplId)."".$tplFile.".php"; $this->write_file($compileFile,$compileContent);#将编译后的内容写入响应的文件中; @extract($this->var); ob_start(); include_once($compileFile); $content = ob_get_contents() ; ob_end_clean() ; if ($this->isCache){ $this->write_file($cacheFile,$content) ;# 帮编译好的内容写入缓存文件 } echo $content ; } } /* function trim_tag($content) { $content = str_replace($this->startTag,"",$content); $content = str_replace($this->endTag,"",$content); //$content = trim($content); return $content ; }*/ # 编译文件函数 function compile_file($content=null) { $content = $content ? $content :die("Compile fail!") ; //$content = $this->trim_tag($content); $content = preg_replace($this->pattern,$this->replacement,$content); return $content; } #剖析包括文件 function inc_file($filename,$tplId="",$cacheId="",$cacheLeftTime="") { $file = $this->path."/".$this->tplDir."/".$filename ; if (file_exists($file)) { $filename = str_replace(".".$this->tplExt,"",$filename); return $this->display($filename,$tplId,$cacheId,$cacheLeftTime); } else die("Template "{$filename}" not exist"); } #读取文件内容函数 function read_file($filename) { if (!file_exists($filename)) die("Read file fail") ; return file_get_contents($filename); } #内容写入函数 function write_file($filename,$content,$mode="wb") { $filename = trim($filename); $content = $content ? stripslashes(trim($content)) : exit(); if (!file_exists($filename)) { $array = explode("/",$filename); $count = count($array); $path = ""; for ($i = 0 ; $i <$count-1 ; ++$i ) { if(!file_exists($path .= $array[$i]."/")) { mkdir($path,0777); } } } $handle = fopen($filename,$mode) ; fwrite($handle,$content); fclose($handle); return true; } # 肃清缓存 function clear_dir($dir="") { $dir = $this->path."/".$dir; $handle = opendir($dir); if (file_exists($dir)) { while ($file = readdir($handle)) { if ($file !="." && $file != "..") unlink($dir."/".$file); } closedir($handle); return true; } else { return false; } } #肃清全部缓存 function clear_all_cache() { if ($this->clear_dir($this->cacheDir) && $this->clear_dir($this->compileDir)) return true; } #检讨缓存能否过时 function check_cache($cacheFile,$cacheLeftTime="") { $cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime; if (!file_exists($cacheFile)) return false ; $time = $this->get_time($cacheFile) + $cacheLeftTime ; if ($time <time()) { unlink($cacheFile); return false; } return true; } # 猎取文件最初编纂时光 function get_time($filename) { if (!file_exists($filename)) return false; return filemtime($filename); } }

上一篇:使用PHP的错误处理-网络编程

下一篇:没有了