3 Pixmicat! Template-Embedded Library v070618
\r
5 Copyright(C) 2005-2007 Pixmicat! Development Team
\r
7 Pixmicat! Template-Embedded Library (PTE) is released under The Clarified
\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
13 $Id: lib_pte.php 438 2007-06-18 13:59:13Z roytam1 $
\r
17 var $tpl_block, $tpl;
\r
20 function PTELibrary($tplname){
\r
21 $this->tpl_block = array();
\r
22 $this->tpl = file_get_contents($tplname);
\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
31 $this->tpl_block[$blockName] = false; // 找過但沒找到
\r
33 return $this->tpl_block[$blockName];
\r
36 /* 回傳去除前後空格的區塊樣板碼 */
\r
37 function BlockValue($blockName){
\r
38 return trim($this->_readBlock($blockName));
\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
51 function EvalIF($tpl, $ary){
\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
62 function EvalFOREACH($tpl, $ary){
\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
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
78 function EvalInclude($tpl, $ary){
\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