]> 4ch.mooo.com Git - test.git/blob - lib/lib_pte.php
modified: config.php
[test.git] / lib / lib_pte.php
1 <?php\r
2 /*\r
3 Pixmicat! Template-Embedded Library v070618\r
4 by: scribe & RT\r
5 Copyright(C) 2005-2007 Pixmicat! Development Team\r
6 \r
7 Pixmicat! Template-Embedded Library (PTE) is released under The Clarified \r
8 Artistic License.\r
9 A more detailed definition of the terms please refer to the attached "LICENSE" \r
10 file. If you do not receive the program with The Artistic License copy, please \r
11 visit http://pixmicat.openfoundry.org/license/ to obtain a copy.\r
12 \r
13 $Id: lib_pte.php 438 2007-06-18 13:59:13Z roytam1 $\r
14 */\r
15 \r
16 class PTELibrary{\r
17         var $tpl_block, $tpl;\r
18 \r
19         /* 開啟樣板檔案並取出區塊 */\r
20         function PTELibrary($tplname){\r
21                 $this->tpl_block = array();\r
22                 $this->tpl = file_get_contents($tplname);\r
23         }\r
24 \r
25         /* 回傳區塊樣板碼並快取 */\r
26         function _readBlock($blockName){\r
27                 if(!isset($this->tpl_block[$blockName])){ // 是否找過\r
28                         if(preg_match('/<!--&'.$blockName.'-->(.*)<!--\/&'.$blockName.'-->/smU', $this->tpl, $matches))\r
29                                 $this->tpl_block[$blockName] = $matches[1]; // 找到了存入陣列快取\r
30                         else\r
31                                 $this->tpl_block[$blockName] = false; // 找過但沒找到\r
32                 }\r
33                 return $this->tpl_block[$blockName];\r
34         }\r
35 \r
36         /* 回傳去除前後空格的區塊樣板碼 */\r
37         function BlockValue($blockName){\r
38                 return trim($this->_readBlock($blockName));\r
39         }\r
40 \r
41         /* 將樣版的標籤取代為正確的字串並傳回 */\r
42         function ParseBlock($blockName, $ary_val){\r
43                 if(($tmp_block = $this->_readBlock($blockName))===false) return ""; // 找無\r
44                 $tmp_block = $this->EvalFOREACH($tmp_block, $ary_val); // 解析FOREACH敘述\r
45                 $tmp_block = $this->EvalIF($tmp_block, $ary_val); // 解析IF敘述\r
46                 $tmp_block = $this->EvalInclude($tmp_block, $ary_val); // 解析引用\r
47                 return @str_replace(@array_keys($ary_val), @array_values($ary_val), $tmp_block);\r
48         }\r
49 \r
50         /* 解析IF敘述 */\r
51         function EvalIF($tpl, $ary){\r
52                 $tmp_tpl = $tpl;\r
53                 if(preg_match_all('/<!--&IF\(([\$&].*),\'(.*)\',\'(.*)\'\)-->/smU', $tmp_tpl, $matches, PREG_SET_ORDER)){\r
54                         foreach($matches as $submatches){\r
55                                 $isblock = substr($submatches[1],0,1) == "&"; $vari = substr($submatches[1],1); $iftrue = $submatches[2]; $iffalse = $submatches[3];\r
56                                 $tmp_tpl = @str_replace($submatches[0], (($isblock ? $this->BlockValue($vari) : ($ary['{$'.$vari.'}'] !== '' && $ary['{$'.$vari.'}'] !== false && $ary['{$'.$vari.'}'] !== null)) ? $this->EvalInclude($iftrue, $ary) : $this->EvalInclude($iffalse, $ary)), $tmp_tpl);\r
57                         }\r
58                 }\r
59                 return $tmp_tpl;\r
60         }\r
61         /* 解析FOREACH敘述 */\r
62         function EvalFOREACH($tpl, $ary){\r
63                 $tmp_tpl = $tpl;\r
64                 if(preg_match_all('/<!--&FOREACH\((\$.*),\'(.*)\'\)-->/smU', $tmp_tpl, $matches, PREG_SET_ORDER)){\r
65                         foreach($matches as $submatches){\r
66                                 $vari = $submatches[1]; $block = $submatches[2];\r
67                                 \r
68                                 $foreach_tmp = '';\r
69                                 if(isset($ary['{'.$vari.'}']) && is_array($ary['{'.$vari.'}']))\r
70                                         foreach($ary['{'.$vari.'}'] as $eachvar)\r
71                                                 $foreach_tmp .= $this->ParseBlock($block, $eachvar);\r
72                                 $tmp_tpl = @str_replace($submatches[0], $foreach_tmp, $tmp_tpl);\r
73                         }\r
74                 }\r
75                 return $tmp_tpl;\r
76         }\r
77         /* 解析區塊引用 */\r
78         function EvalInclude($tpl, $ary){\r
79                 $tmp_tpl = $tpl;\r
80                 if(preg_match_all('/<!--&(.*)\/-->/smU', $tmp_tpl, $matches, PREG_SET_ORDER))\r
81                         foreach($matches as $submatches)\r
82                                 $tmp_tpl = str_replace($submatches[0], $this->ParseBlock($submatches[1], $ary), $tmp_tpl);\r
83                 return $tmp_tpl;\r
84         }\r
85 }\r
86 ?>