pixmicat - Blame information for rev 438

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