From cd3f2af96ea8573861df2fb2c34342c662b355d6 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 25 Jul 2005 00:51:27 +0000 Subject: [PATCH] Else tags are now working again. The problem was that if in a nested , one of the children had an but the parent didn't, then it would break. --- template.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/template.php b/template.php index a4b73a9..03341ad 100644 --- a/template.php +++ b/template.php @@ -464,15 +464,7 @@ class DB_Template // tag stack $stack = array(); $parsed = array(); - - // replace tags with tokens - $token_start = '<-|{['; - $token_end = ']}|->'; - - $template = str_replace($tag_start, $token_start . 'TT_IF' . $token_end . '(', $template); - $template = preg_replace('#' . preg_quote('<-|{[TT_IF]}|->(', '#') . '(.*?)' . preg_quote($tag_start_end, '#') . '#', '<-|{[TT_IF]}|->(' . '\\1)<-|{[TT_EIF]}|->', $template); - $template = str_replace($tag_else, $token_start . 'TT_ELSE' . $token_end, $template); - $template = str_replace($tag_end, $token_start . 'TT_ENDIF' . $token_end, $template); + $elsestack = array(); for ($i = 0; $i < strlen($template); $i++) { @@ -484,6 +476,11 @@ class DB_Template //echo "OPEN `$i`: " . print_r($stack, true); } + // locate else tags + else if (substr($template, $i, strlen($tag_else)) == $tag_else) + { + array_push($elsestack, end($stack)); + } // do we have an end tag? else if (substr($template, $i, strlen($tag_end)) == $tag_end) { @@ -522,13 +519,13 @@ class DB_Template 'parsed' => '' ); - if (substr_count($conditional, $tag_else) == 0) + if (end($elsestack) == $last) { - $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '" : "") . "'; + $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '") . "'; } else { - $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '") . "'; + $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '" : "") . "'; } } } -- 2.22.5