[Smarttemplate-cvscommit] smarttemplate class.smarttemplateparser.php, 1.15, 1.16
Brought to you by:
codeworxtech
|
From: EndelWar <end...@us...> - 2006-10-21 19:56:40
|
Update of /cvsroot/smarttemplate/smarttemplate In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv32060 Modified Files: class.smarttemplateparser.php Log Message: Replace template logical expression in IF..ENDIF (|| or &&) with php logical expression. Patch by Bruce Huang Index: class.smarttemplateparser.php =================================================================== RCS file: /cvsroot/smarttemplate/smarttemplate/class.smarttemplateparser.php,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** class.smarttemplateparser.php 7 Oct 2006 09:21:16 -0000 1.15 --- class.smarttemplateparser.php 21 Oct 2006 19:56:36 -0000 1.16 *************** *** 81,84 **** --- 81,154 ---- /** + * replace template logical expression in IF..ENDIF (|| or &&) with php logical expression + * + * @access private + * @desc replace template logical expression (|| or &&) with php logical expression + * @author Bruce Huang (msn: hua...@16...) + * @param string $src_page source page intended to be replaced + * + */ + function replace_logic_expression( &$src_page ) + { + //cannot find "||" or "&&" + if(!strpos($src_page, '||') && !strpos($src_page, '&&')) + { + return; + } + //match 'ELSE' and the last sub expression + if (preg_match_all('/<!-- (ELSE)?IF [ ]*(\(*).*[|&]{2}[ ]*\(*[ ]*([a-zA-Z0-9_.]+)[ ]*([!=<>]+)[ ]*(["]?[^"]*?["]?)[ ]*(\)*)[ ]* -->/', $src_page, $var)) + { + foreach ($var[3] as $cnt => $tag) + { + list($parent, $block) = $this->var_name($tag); + $cmp = $var[4][$cnt]; + $val = $var[5][$cnt]; + $else = ($var[1][$cnt] == 'ELSE') ? '} else' : ''; + if ($cmp == '=') + { + $cmp = '=='; + } + if (preg_match('/"([^"]*)"/',$val,$matches)) + { + $code_suffix = "\$$parent"."['$block'] $cmp \"".$matches[1].$var[6][$cnt]."\"){\n?>"; + } + elseif (preg_match('/([^"]*)/',$val,$matches)) + { + list($parent_right, $block_right) = $this->var_name($matches[1]); + $code_suffix = "\$$parent"."['$block'] $cmp \$$parent_right"."['$block_right']".$var[6][$cnt]."){\?>"; + } + + //match other sub expressions + if (preg_match_all('/([a-zA-Z0-9_.]+)[ ]*([!=<>]+)[ ]*(["]?[^"]*?["]?)[ ]*(\)*[ ]*[|&]{2}[ ]*\(*)[ ]*/', $var[0][$cnt], $sub_var)) + { + $code_mid = ''; + foreach($sub_var[1] as $sub_cnt => $sub_tag) + { + list($sub_parent, $sub_block) = $this->var_name($sub_tag); + $cmp = $sub_var[2][$sub_cnt]; + $val = $sub_var[3][$sub_cnt]; + $logic_exp = $sub_var[4][$sub_cnt]; + if ($cmp == '=') + { + $cmp = '=='; + } + if (preg_match('/"([^"]*)"/',$val,$matches)) + { + $code_mid = $code_mid."\$$sub_parent"."['$sub_block'] $cmp \"".$matches[1]."\"".$logic_exp; + } + elseif (preg_match('/([^"]*)/',$val,$matches)) + { + list($sub_parent_right, $sub_block_right) = $this->var_name($matches[1]); + $code_mid = $code_mid."\$$sub_parent"."['$sub_block'] $cmp \$$sub_parent_right"."['$sub_block_right']".$logic_exp; + } + } + } + $code = "<?php\n".$else.'if ('.$var[2][$cnt].$code_mid.$code_suffix; + $src_page = str_replace($var[0][$cnt], $code, $src_page); + } + } + } + + /** * Main Template Parser * *************** *** 161,164 **** --- 231,237 ---- } + // replace logical operator in [ELSE]IF + $this->replace_logic_expression($page); + // 'IF nnn=mmm' Blocks if (preg_match_all('/<!-- (ELSE)?IF ([a-zA-Z0-9_.]+)[ ]*([!=<>]+)[ ]*(["]?[^"]*["]?) -->/', $page, $var)) |