Tyds simple template parser php class |
[eluser]Unknown[/eluser]
Tyds simple template parser PHP class is a wrapper class / library. It is a lightweight but powerful tool used to isolate Web applications back-end business logic design and front-end page layout. Full code text file download Location: https://skydrive.live.com/?cid=a29a583f7...061234!872 <?php /********************************************* * Application name:Tyds simple template parser php class * version: v1.0.2 * Author: tyds msn: [email protected] * email: [email protected] ***********************************************/ // use it with CodeIgniter , delete "//" at the left of the next line. // if (! defined('BASEPATH')) exit('No direct script access allowed'); class Tydsparser { var $left_delimiter = '{'; var $right_delimiter = '}'; var $template_dir = 'microtpl'; var $compile_dir = 'cache'; var $html_dir = 'cache/html'; var $force_compile = false; var $force_html =false; var $exp_time = 0; var $html_time = 0; var $d_time=0; var $ftpl_var = array(); var $starttime =0.00; var $char_code_re = ''; public function __construct($config = array()){ $this->initialize($config); // use it with CodeIgniter , delete "//" at the left of the next line. // log_message('debug', "Tydsparser Class Initialized"); } private function initialize($config = array()) { $path_parts = pathinfo(__FILE__); $basedir = $path_parts["dirname"] ; clearstatcache(); if (count($config) > 0) { if(!empty($config['left_tag'])&&!empty($config['right_tag'])){ $this->left_delimiter=$config['left_tag']; $this->right_delimiter=$config['right_tag']; } if (!empty($config['template_dir'])){ $this->template_dir=THEROOT.$config['template_dir']; }else{$this->template_dir=THEROOT.$this->template_dir;} if (!empty($config['compile_dir'])&&!empty($config['html_dir'])) { $this->compile_dir=THEROOT.$config['compile_dir']; $this->html_dir=THEROOT.$config['html_dir']; }else{ $this->compile_dir=THEROOT.$this->compile_dir; $this->html_dir=THEROOT.$this->html_dir; } if (!empty($config['exp_time'])&&preg;_match('/\d+/', $config['exp_time'])) { $this->exp_time=$config['exp_time']; } if (!empty($config['html_time'])&&preg;_match('/\d+/', $config['html_time'])) { $this->exp_time=$config['html_time']; } if (!empty($config['datestr'])){ date_default_timezone_set(PRC); $Dstrp = '/^\s*([1-9]\d{3})\s*[\-]?\s*(0?[1-9]|1[0-2])?\s*[\-]?\s*([0-2]?\d|3[01])?\s*([0-1]?\d|2[0-3])?\s*[:]?\s*([0-5]?\d)?\s*[:]?\s*([0-5]?\d)?$/'; if (preg_match($Dstrp,$config['datestr'],$time_arr)){ list($all, $Y,$M,$d,$H,$m,$S) = $time_arr; $nowstr = date("Y-m-d H:i ![]() preg_match($Dstrp ,$nowstr,$now_arr); list($alln, $Yn,$Mn,$dn,$Hn,$mn,$Sn) = $now_arr; $Y=is_null($Y)?$Yn:$Y; $M=is_null($M)?$Mn:$M; $d=is_null($d)?$dn:$d; $H=is_null($H)?$Hn:$H; $m=is_null($m)?$mn:$m; $S=is_null($S)?$Sn:$S; $ms = mktime($H,$m,$S,$M,$d,$Y); $this->d_time=$ms; } } } $re["gbk"] = "[\x20-\x7e]|[\x81-\xfe][\x40-\xfe]"; $re["gb2312"] = "[\x20-\x7e]|[\xb0-\xf7][\xa0-\xfe]"; $re["utf8"] = "[\x20-\x7e]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}"; $re["big5"] = "[\x20-\x7e]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])"; $this->char_code_re = $re["utf8"]; $this->create($this->compile_dir); $this->create($this->html_dir); } public function __destruct(){ unset($this->_section,$this->ftpl_var); } private function mysubstr($str, $strlen, $suffix='') { preg_match_all( '/'.$this->char_code_re.'/', $str, $match); if(count($match[0]) <= $strlen) return $str; $slice = join("",array_slice($match[0], 0, $strlen)); $slice = $slice.$suffix; return $slice; } public function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } private function var_or_value($equalvalue) { $equalvalue = trim($equalvalue); if(preg_match('/^\$\w([\w\[\]\$]*[\w\]])*/',$equalvalue)) {return $this->parse_vars($equalvalue); } elseif(preg_match('/^[\'](.+)[\']$/',$equalvalue,$matches)) {return $equalvalue; } elseif(preg_match('/^["](.+)["]$/',$equalvalue,$matches)) { return $equalvalue; } elseif(preg_match('/^\d+$/',$equalvalue,$matches)) {return $equalvalue; } elseif(strtolower($equalvalue) == "true" || strtolower($equalvalue) == "false" || strtolower($equalvalue) == "null") {return $equalvalue; } elseif(strlen($equalvalue) == 0) {return "''"; } else{ $equalvalue =str_replace("\'", "'", $equalvalue); $equalvalue ="'".str_replace("'", "\'", $equalvalue)."'"; return $equalvalue; } } protected function create($dir){ if (!is_dir($dir)) { $temp = explode('/',$dir); $cur_dir = ''; for($i=0;$i<count($temp);$i++) { $cur_dir .= $temp[$i].'/'; if (!is_dir($cur_dir)) { @mkdir($cur_dir,0777); @fopen("$cur_dir/index.htm","a"); } } } } private function write_file($file_name,$content){ $compiledfile_url = $this->get_compiledfile_url($file_name); if(!is_dir($this->compile_dir)){ $this->create($this->compile_dir); } if(!is_writable($this->compile_dir)){ $this->show_messages("Please set read/write property of the compile directory:".$this->compile_dir." and files in it as 777。"); } $fp = fopen($compiledfile_url,'wb'); fwrite($fp,$content); fclose($fp); chmod($compiledfile_url,0777); } public function show_messages($message){ echo $message; exit; } ...... |
Welcome Guest, Not a member yet? Register Sign In |