From b90c1ebf87cd01f7841da91c765c87aac0b7f332 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 19 Mar 2005 05:26:19 +0000 Subject: [PATCH] Merging tpl-if-fix/ branch into trunk. We now properly handle nested conditionals with our bran-spankin' new parser. --- template.php | 137 ++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 72 deletions(-) diff --git a/template.php b/template.php index 17bde98..71c4f8c 100644 --- a/template.php +++ b/template.php @@ -390,94 +390,87 @@ class DB_Template */ function _parse_conditionals($template) { - // Tag locations - $location_start = -1; - $location_end = -1; + global $_isso; - // Tag data - $tag_start = ' 1) + // we've found ourselves a conditional! + if (substr($template, $i, strlen($tag_start)) == $tag_start) { - $location_start = $location_start + 1; - $location_end = $location_start + 2; + // where do we end the condition + $end = strpos($template, $tag_start_end, $i); + $end += strlen($tag_start_end); + + // extract the condition + $condition = substr($template, $i, $end - $i); + + // complete the open + array_push($stack, $i); + + //echo "OPEN `$i`: " . print_r($stack, true); } - - // Strip out the opening ' 2) - { - trigger_error('Multiple else statements encountered while parsing conditionals in template', ERR_ALERT); - } + // find the most recently opened condition + $last = array_pop($stack); + $depth = count($stack); + + //echo "CLOSE `$last`: " . print_r($stack, true); + + // get the full condition + $fullspread = substr($template, $last, $end - $last); + + // remove the beginning tag + $conditional = substr($fullspread, strlen($tag_start)); - // Set the data - $iftrue = $data[0]; - $iffalse = $data[1]; + // find the end of the expression + $temp_end = strpos($conditional, $tag_start_end); + + // save the expression + $expression = stripslashes(substr($conditional, 0, $temp_end)); + + // remove the condition from the broken conditional + $conditional = substr($conditional, strlen($expression) + strlen($tag_start_end)); + + // remove the tailing end tag + $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end)); + + // save all the data for later so we can do a quick replacement + $parsed["$depth"]["$last"] = array( + 'raw' => $fullspread, + 'parsed' => '" . iff(' . stripslashes($expression) . ',"' . $conditional . '") . "', + ); } - // Nope, reassign variables - else + } + + ksort($parsed); + foreach ($parsed AS $depth => $conditionals) + { + ksort($conditionals); + foreach ($conditionals AS $parsed) { - $iftrue = $parsed; - $iffalse = null; + $template = str_replace($parsed['raw'], $parsed['parsed'], $template); } - - // Put the condition and iftrue in the iff() function - $parsed_expression = '" . iff(' . stripslashes($expression_condition) . ',"' . $iftrue . '","' . $iffalse . '") . "'; - - // Parse the iff()'d expression back into the template data - $template = substr_replace($template, $parsed_expression, $location_start, $location_end + strlen($tag_end) - $location_start); } + $template = str_replace('', '","', $template); - // Repeat this process until it can't find any more - // expressions, then send back the parsed template data - // for final output return $template; } } -- 2.22.5