-
- /*
- * 클래스: 模板解析类
- * 작성자: 51JS.COM-ZMM
- * 날짜: 2011.3.1
- * 이메일: 304924248@qq.com
- * 블로그: http://www.cnblogs.com/cnzmm/
- * 링크: http://bbs.it-home.org
- */
- 클래스 템플릿 {
- public $html, $vars, $bTag, $eTag;
- 공개 $bFlag='{', $eFlag='}', $pfix='zmm:';
- 비공개 $폴더, $file;
- function __construct($vars=array()) {
- !empty($vars) && $this->vars = $vars;
- !empty($GLOBALS['cfg_tag_prefix']) &&
- $this->pfix = $GLOBALS['cfg_tag_prefix'].':';
- $this->bTag = $this->bFlag.$this->pfix;
- $this->eTag = $this->bFlag.'/'.$this->pfix;
- empty(Tags::$vars) && Tags::$vars = &$this->vars;
- }
- 공용 함수 LoadTpl($tpl) {
- $this->file = $this->GetTplPath($tpl);
- 태그::$file = &$this->file;
- if (is_file($this->file)) {
- if ($this->GetTplHtml()) {
- $this->SetTplTags();
- } else {
- exit('模板文件加载失败!');
- }
- } else {
- exit('模板文件['.$this->file.']불存재!');
- }
- }
- 비공개 함수 GetTplPath($tpl) {
- $this->folder = WEBSITE_DIRROOT.
- $GLOBALS['cfg_tpl_root'];
- $this->폴더를 반환합니다.'/'.$tpl;
- }
- 비공개 함수 GetTplHtml() {
- $html = self::FmtTplHtml(file_get_contents($this->file));
- if (!empty($html)) {
- $callFunc = Tags::$prefix.'구문';
- $this->html = Tags::$callFunc($html, new Template());
- } else {
- exit('模板文件内容为空!');
- } true를 반환합니다.
- }
- 정적 공개 함수 FmtTplHtml($html) {
- return preg_replace('/(r)|(n)|(t)|(s{2,})/is', '', $html);
- }
- 공용 함수 Register($vars=array()) {
- if (is_array($vars)) {
- $this->vars = $vars;
- 태그::$vars = &$this->vars;
- }
- }
- 공용 함수 Display($bool=false, $name="", $time=0) {
- if (!empty($this->html)) {
- if ($bool && !empty($name)) {
- if (!is_int($time)) $time = 600;
- $cache = 새 캐시($time);
- $cache->Set($name, $this->html);
- }
- echo $this->html; 플러시();
- } else {
- exit('模板文件内容为空!');
- }
- }
- 공용 함수 SetAssign($souc, $info) {
- if (!empty($this->html)) {
- $this->html = str_ireplace ($souc, self::FmtTplHtml($info), $this->html);
- } else {
- exit('模板文件内容为空!');
- }
- }
- 비공개 함수 SetTplTags() {
- $this->SetPanelTags(); $this->SetTrunkTags(); $this->RegHatchVars();
- }
- 비공개 함수 SetPanelTags() {
- $rule = $this->bTag.'([^'.$this->eFlag.'] )/'.$this-> e플래그;
- preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
- $this->TransTag($out_matches, '패널'); unset($out_matches);
- }
- 비공개 함수 SetTrunkTags() {
- $rule = $this->bTag.'(w )s*([^'.$this->eFlag.']*?)' .$this->eFlag.
- '((?:(?!'.$this->bTag.')[Ss]*?|(?R))*)'.$this->eTag.'\1s*'. $this->eFlag;
- preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
- $this->TransTag($out_matches, '트렁크'); unset($out_matches);
- }
- 비공개 함수 TransTag($result, $type) {
- if (!empty($result[0])) {
- switch ($type) {
- case 'panel' : {
- for ($i = 0; $i $strTag = 폭발(' ', $result[1][$i], 2);
- if (strpos($strTag[0], '.')) {
- $itemArg =Explode('.', $result[1][$i], 2);
- $callFunc = 태그::$prefix.ucfirst($itemArg[0]);
- if (method_exists('Tags', $callFunc)) {
- $html = Tags::$callFunc(chop($itemArg[1]));
- if ($html !== false) {
- $this->html = str_ireplace($result[0][$i], $html, $this->html);
- }
- }
- } else {
- $rule = '^([^s] )s*([Ss] )$';
- preg_match_all('/'.$rule.'/is', Trim($result[1][$i]), $tmp_matches);
- $callFunc = 태그::$prefix.ucfirst($tmp_matches[1][0]);
- if (method_exists('Tags', $callFunc)) {
- $html = Tags::$callFunc($tmp_matches[2][0]);
- if ($html !== false) {
- $this->html = str_ireplace($result[0][$i], $html, $this->html);
- }
- } unset($tmp_matches);
- }
- } 휴식;
- }
- case '트렁크': {
- for ($i = 0; $i < count($result[0]); $i ) {
- $callFunc = 태그::$ prefix.ucfirst($result[1][$i]);
- if (method_exists('Tags', $callFunc)) {
- $html = Tags::$callFunc($result[2][$i], $result[3][$i]);
- $this->html = str_ireplace($result[0][$i], $html, $this->html);
- }
- } 휴식;
- }
- 기본값: 중단;
- }
- } else {
- false를 반환합니다.
- }
- }
- 비공개 함수 RegHatchVars() {
- $this->SetPanelTags();
- }
- function __destruct() {}
- }
- ?>
复代码
2. 태그 파싱 클래스 : (데이터 및 목록 태그에 대한 파싱 아이디어를 일시적으로 제공)
-
- /*
- * 클래스: 标签解析类
- * 작성자: 51JS.COM-ZMM
- * 날짜: 2011.3.2
- * 이메일: 304924248@qq.com
- * 블로그: http://www.cnblogs.com/cnzmm/
- * 링크: http://bbs.it-home.org
- */
- class 태그 {
- static private $attrs=null;
- 정적 공개 $file, $vars, $rule, $prefix='TAG_';
- 정적 공개 함수 TAG_Syntax($html, $that) {
- $rule = $that->bTag.'ifs ([^'.$that->eFlag.'] )s*'.$ that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- $rule = $that->bTag.'elseifs ([^'.$that->eFlag.'] )s*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- $rule = $that->bTag.'elses*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- $rule = $that->bTag.'loops (S )s (S )s*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- $rule = $that->bTag.'loops (S )s (S )s (S )s*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', ' \3) { ?>', $html);
- $rule = $that->eTag.'(if|loop)s*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- $rule = $that->bTag.'phps*'.$that->eFlag.'((?:(?!'.
- $that->bTag.')[Ss] *?|(?R))*)'.$that->eTag.'phps*'.$that->eFlag;
- $html = preg_replace('/'.$rule.'/ism', '', $html);
- return self::TAG_Execute($html);
- }
- 정적 공개 함수 TAG_List($attr, $html) {
- if (!empty($html)) {
- if (self::TAG_HaveTag($html)) {
- return self::TAG_DealTag($attr, $html, true);
- } else {
- return self::TAG_GetData($attr, $html, true);
- }
- } else {
- exit('标签{list}的内容为空!');
- }
- }
- 정적 공개 함수 TAG_Data($attr, $html) {
- if (!empty($html)) {
- if (self::TAG_HaveTag($html)) {
- return self::TAG_DealTag($attr, $html, false);
- } else {
- return self::TAG_GetData($attr, $html, false);
- }
- } else {
- exit('标签{data}的内容为空!');
- }
- }
- 정적 공개 함수 TAG_Execute($html) {
- ob_clean(); ob_start();
- if (!empty(self::$vars)) {
- is_array(self::$vars) &&
- extract(self::$vars, EXTR_OVERWRITE);
- }
- $file_inc = WEBSITE_DIRINC.'/buffer/'.
- md5(uniqid(rand(), true)).'.php';
- if ($fp = fopen($file_inc, 'xb')) {
- fwrite($fp, $html);
- if (fclose($fp)) {
- include($file_inc);
- $html = ob_get_contents();
- } 설정 해제($fp);
- } else {
- exit('模板解析文件生成失败!');
- } ob_end_clean(); @unlink($file_inc);
- $html을 반환합니다.
- }
- 정적 비공개 함수 TAG_HaveTag($html) {
- $bool_has = false;
- $tpl_ins = 새 템플릿();
- self::$rule = $tpl_ins->bTag.'([^'.$tpl_ins->eFlag.'] )/'.$tpl_ins->eFlag;
- $bool_has = $bool_has || preg_match('/'.self::$rule.'/ism', $html);
- self::$rule = $tpl_ins->bTag.'(w )s*([^'.$tpl_ins->eFlag.']*?)'.$tpl_ins->eFlag.
- '((?:(?!'.$tpl_ins->bTag.')[Ss]*?|(?R))*)'.$tpl_ins->eTag.'\1s*'. $tpl_ins->eFlag;
- $bool_has = $bool_has || preg_match('/'.self::$rule.'/ism', $html);
- 설정 해제($tpl_ins);
- $bool_has를 반환합니다.
- }
- 정적 비공개 함수 TAG_DealTag($attr, $html, $list) {
- preg_match_all('/'.self::$rule.'/ism', $html, $out_matches);
- if (!empty($out_matches[0])) {
- $child_node = array();
- for ($i = 0; $i $child_node[] = $out_matches[3][$i];
- $html = str_ireplace($out_matches[3][$i], '{-->>child_node_'.$i.'<<--}', $html);
- }
- $html = self::TAG_GetData($attr, $html, $list);
- for ($i = 0; $i < count($out_matches[0]); $i ) {
- $html = str_ireplace('{-->>child_node_'.$i.' <<--}', $child_node[$i], $html);
- }
- preg_match_all('/'.self::$rule.'/ism', $html, $tmp_matches);
- if (!empty($tmp_matches[0])) {
- for ($i = 0; $i < count($tmp_matches[0]); $i ) {
- $callFunc = self ::$prefix.ucfirst($tmp_matches[1][$i]);
- if (method_exists('Tags', $callFunc)) {
- $temp = self::$callFunc($tmp_matches[2][$i], $tmp_matches[3][$i]);
- $html = str_ireplace($tmp_matches[0][$i], $temp, $html);
- }
- }
- }
- unset($tmp_matches);
- }
- unset($out_matches); $html을 반환합니다.
- }
- 정적 비공개 함수 TAG_GetData($attr, $html, $list=false) {
- if (!empty($attr)) {
- $attr_ins = new Attbt($attr);
- $attr_arr = $attr_ins->attrs;
- if (is_array($attr_arr)) {
- extract($attr_arr, EXTR_OVERWRITE);
- $source = 테이블_이름($source, $column);
- $rule = '[필드:s*(w )s*([^]]*?)s*/?]';
- preg_match_all('/'.$rule.'/is', $html, $out_matches);
- $data_str = '';
- $data_ins = 새로운 DataSql();
- $attr_where = $attr_order = '';
- if (!empty($where)) {
- $where = str_replace(',', ' and ', $where);
- $attr_where = '어디'. $어디서;
- }
- if (!empty($order)) {
- $attr_order = '주문 기준'.$order;
- } else {
- $fed_name = '';
- $fed_ins = $data_ins->GetFedNeedle($source);
- $fed_cnt = $data_ins->GetFedCount($fed_ins);
- for ($i = 0; $i $fed_flag = $data_ins->GetFedFlag($fed_ins, $i);
- if (preg_match('/auto_increment/ism', $fed_flag)) {
- $fed_name = $data_ins->GetFedName($fed_ins, $i);
- 휴식;
- }
- }
- if (!empty($fed_name))
- $attr_order = ''.$fed_name으로 주문하세요.' 설명';
- }
- if ($list == true) {
- if (empty($source) &&empty($sql)) {
- exit('标签{list}必须指정source属性!' );
- }
- $attr_rows = $attr_page = '';
- if ($rows > 0) {
- $attr_rows = ' 제한 0,'.$rows;
- }
- if (!empty($sql)) {
- $data_sql = $sql;
- } else {
- $data_sql = '`'.$source.'`에서 *를 선택하세요.
- $attr_where.$attr_order.$attr_rows;
- }
- if ($pages=='true' && !empty($size)) {
- $data_num = $data_ins->GetRecNum($data_sql);
- $page_cnt = ceil($data_num / $size);
- 글로벌 $page;
- if (!isset($page) || $page < 1) $page = 1;
- if ($page > $page_cnt) $page = $page_cnt;
- $data_sql = '``.$source.'`'.$attr_where에서 *를 선택하세요.
- $attr_order.' 제한 '.($page-1) * $size.','.$size;
- $GLOBALS['cfg_page_curr'] = $페이지;
- $GLOBALS['cfg_page_prev'] = $페이지 - 1;
- $GLOBALS['cfg_page_next'] = $페이지 1;
- $GLOBALS['cfg_page_nums'] = $page_cnt;
- if (function_exists('list_pagelink')) {
- $GLOBALS['cfg_page_list'] = list_pagelink($page, $page_cnt, 2);
- }
- }
- $data_idx = 0;
- $data_ret = $data_ins->SqlCmdExec($data_sql);
- while ($row = $data_ins->GetRecArr($data_ret)) {
- if ($skip > 0 && !empty($flag)) {
- $data_idx != 0 &&
- $data_idx % $skip == 0 &&
- $data_str .= $flag;
- }
- $data_tmp = $html;
- $data_tmp = str_ireplace('@idx', $data_idx, $data_tmp);
- for ($i = 0; $i $data_tmp = str_ireplace($out_matches[0][$i],
- $row [$out_matches[1][$i]], $data_tmp);
- }
- $data_str .= $data_tmp; $data_idx ;
- }
- } else {
- if (empty($source)) {
- exit('标签{data}必须指정source属性!');
- }
- $data_sql = '``.$source에서 *를 선택하세요.
- '`'.$attr_where.$attr_order;
- $row = $data_ins->GetOneRec($data_sql);
- if (is_array($row)) {
- $data_tmp = $html;
- for ($i = 0; $i $data_val = $row[$out_matches[1][$i]];
- if (empty($out_matches[2][$i])) {
- $data_tmp = str_ireplace($out_matches[0][$i], $data_val, $data_tmp);
- } else {
- $attr_str = $out_matches[2][$i];
- $attr_ins = 새 속성($attr_str);
- $func_txt = $attr_ins->attrs['function'];
- if (!empty($func_txt)) {
- $func_tmp = 폭발('(', $func_txt);
- if (function_exists($func_tmp[0])) {
- eval(' $func_ret ='.str_ireplace('@me',
- '''.$data_val.''', $func_txt))
- $data_tmp = str_ireplace($out_matches[0][$i], $ func_ret, $data_tmp);
- } else {
- exit('调用了不存재적函数!')
- }
- } else {
- exit('标签设置属无效!' );
- }
- }
- $data_str .= $data_tmp
- }
- }
- unset($data_ins)
- return $data_str; >} else {
- exit('标签设置属性无效!');
- }
- } else {
- exit('没有设置标签属性!')
- }
- }
- 정적 공용 함수 __callStatic($name, $args) {
- exit('标签{'.$name.'}不存재!')
- }
- }
- ?>
-
-
제제대码
|