?? template.inc.php
字號:
<?php/*** Title: Template* File(s): template.inc.php* Author: episome <webmaster@3ants.org>* Modify: milkliker* Website: http://3ants.org/* Version: 0.0.4* Last changed: 21:09 04-8-4** This template class is modifyed from PHPBB2 templates By Nathan Codding* It is free software; you can redistribute it and/or modify..*/class Template {var $show_err_msg = true;var $root = '';var $cacheDir = '';var $files = array();var $_tpldata = array();var $compiled_code = array();var $uncompiled_code = array();/*** Constructor. Simply sets the root dir & cache dir.*/function Template($root = './',$cacheDir = 'cache/'){if (substr($root,-1) != '/')$root.='/';if (substr($cacheDir,-1) != '/')$cacheDir.='/';if (!is_dir($root)) {$this->_halt("set_root: $root is not a directory.");}if (!is_dir($cacheDir) && !@mkdir($cacheDir,0777)){$this->_halt("set cache dir: $cacheDir is not a directory.");}if (!is_writable($cacheDir) && !@chmod($cacheDir,0777)){$this->_halt("set cache dir: $cacheDir is not writeable.");}$this->root = $root;$this->cacheDir = $cacheDir;}function eraser($tagName) {unset($this->_tpldata[$tagName]);}/*** Halt message*/function _halt($msg) {printf("<br><b>Template Error:</b> %s<br>\n", $this->show_err_msg ? $msg :'Halted!');die();}/*** Sets the template filename for handle. 設置模板文件*/function set_file($varname,$filename){$this->files[$varname] = $this->root.$filename;if (!file_exists($this->files[$varname])){$this->_halt('Filename: file ['.$this->files[$varname].'] does not exist.');}}/*** Sets the template filenames for handles. 設置模板文件*/function set_files($varnames){foreach($varnames as $handle=>$filename){$this->set_file($handle,$filename);}}/*** 設置頂級變量,如果變量存在則被覆蓋*/function set_var($varname, $varval){$this->_tpldata['.'][0][$varname] = $varval;}function set_vars($vararray) {foreach($vararray as $key => $val) {$this->_tpldata['.'][0][$key] = $val;}}/*** 設置區塊變量*/function set_block_var($blockname,$varname=null,$varvalue=null){$this->set_block_vars($blockname,array($varname=>$varvalue));}function set_block_vars($blockname, $vararray=null){if (strstr($blockname, '.')){ // Nested block.$blocks = explode('.', $blockname);$blockcount = count($blocks) - 1;$str = &$this->_tpldata;for ($i = 0; $i < $blockcount; $i++){$str = &$str[$blocks[$i]];$str = &$str[count($str) - 1];}$str[$blocks[$blockcount]][] = $vararray;}else{$this->_tpldata[$blockname][] = $vararray;}return true;}/*** 導入模板文件*/function _loadfile($handle){if (isset($this->uncompiled_code[$handle]) and !empty($this->uncompiled_code[$handle])){return true;}if (!isset($this->files[$handle])){$this->_halt("Template->loadfile(): No file specified for handle $handle");}$templateData=@file($this->files[$handle]);foreach($templateData as $value){$str.=trim($value)."\n";}$this->uncompiled_code[$handle] = $str;}/*** 導入模板,編譯模板(如果cache過期或不存在),執行編譯后模板*/function pparse($handle){$cacheFile = $this->cacheDir.'template['.str_replace('/','~',$this->files[$handle]).'].cache.php';if(@filemtime($cacheFile)<@filemtime($this->files[$handle])) {$this->_loadfile($handle);$this->compiled_code[$handle]=$this->_compile($this->uncompiled_code[$handle]); // Actually compile the code now.$fp = fopen($cacheFile,'w+');fwrite($fp,$this->compiled_code[$handle]);fclose($fp);}include_once($cacheFile);}/*** Compiles the given string of code 編譯模板文件*/function _compile($code){// 替換BLOCK里層標簽$varrefs = array();preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs);$varcount = count($varrefs[1]);for ($i = 0; $i < $varcount; $i++){$namespace = $varrefs[1][$i];$varname = $varrefs[3][$i];$new = $this->generate_block_varref($namespace, $varname);$code = str_replace($varrefs[0][$i], $new, $code);}// 替換外層標簽$code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '<?=$this->_tpldata[\'.\'][0][\'\\1\']?>', $code);// 分割$code_lines = explode("\n", $code);$block_nesting_level = 0;$block_names = array();$block_names[0] = '.';$m=array();// 逐行掃描$line_count = count($code_lines);for ($i = 0; $i < $line_count; $i++){$code_lines[$i] = rtrim($code_lines[$i]);if (preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m)){// BEGIN 和 END 在同一行$n = $m;if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n)){$block_nesting_level++;$block_names[$block_nesting_level] = $m[1];if ($block_nesting_level < 2){// Block is not nested. // 非巢狀標簽$code_lines[$i] = preg_replace('#<!-- BEGIN (.*?) -->#','<?/*'.$m[1].'*/foreach((array)$this->_tpldata[\'' .$n[1].'\'] as $'.$m[1].'){?>',$code_lines[$i]);}else{// This block is nested.巢狀標簽$namespace = substr(implode('.', $block_names), 0, -1);$namespace = substr($namespace, 2);$varref = $this->generate_block_data_ref($namespace, false);$code_lines[$i] = preg_replace('#<!-- BEGIN (.*?) -->#','<?/*'.$m[1].'*/foreach((array)'.$varref.' as $'.$m[1].'){?>',$code_lines[$i]);}// We have the end of a block.unset($block_names[$block_nesting_level]);$block_nesting_level--;$code_lines[$i]=preg_replace('#<!-- END (.*?) -->#','<?};/*'.$n[1].'*/?>',$code_lines[$i]);$m = $n;}else{// We have the start of a block.$block_nesting_level++;$block_names[$block_nesting_level] = $m[1];if ($block_nesting_level < 2){// Block is not nested.$code_lines[$i] = preg_replace('#<!-- BEGIN (.*?) -->#','<?/*'.$m[1].'*/foreach((array)$this->_tpldata[\'' .$m[1].'\'] as $'.$m[1].'){?>',$code_lines[$i]);}else{$namespace = implode('.', $block_names);$namespace = substr($namespace, 2);$varref = $this->generate_block_data_ref($namespace, false);$code_lines[$i] = preg_replace('#<!-- BEGIN (.*?) -->#','<?/*'.$m[1].'*/foreach((array)' .$varref.' as $'.$m[1].'){?>',$code_lines[$i]);}}}elseif (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m)){// 結束一個 blockunset($block_names[$block_nesting_level]);$block_nesting_level--;$code_lines[$i]=preg_replace('#<!-- END (.*?) -->#','<?};/*'.$m[1].'*/?>',$code_lines[$i]);}}$code = implode("\n", $code_lines);return $code;}/*** 替換 Block 變量*/function generate_block_varref($namespace, $varname){$namespace = substr($namespace, 0, strlen($namespace) - 1);return '<?='.$this->generate_block_data_ref($namespace, true).'[\''.$varname.'\']?>';}/*** 根據 Block name 取得輸出變量名稱:* 包括上級變量則返回 $parent['self'] 否則返回 $self(用于foreach循環)*/function generate_block_data_ref($blockname, $include_last_iterator=false){$blocks = explode('.', $blockname);$blockcount = count($blocks) - 1;return ($include_last_iterator)? '$'.$blocks[$blockcount] : '$'.$blocks[$blockcount-1].'[\''.$blocks[$blockcount].'\']';}function set_var_from_handle($varname, $handle) {$this->set_var($varname,'<?$this->pparse(\''.$handle.'\')?>');}}?>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -