From d351ec3267e307bba586b9456544735732b9b47c Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 13 Aug 2005 07:04:10 +0000 Subject: [PATCH] We're getting closer witht his method :) --- template.php | 92 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/template.php b/template.php index fa14b6d..5530304 100644 --- a/template.php +++ b/template.php @@ -470,11 +470,9 @@ class DB_Template // tag stack $stack = array(); - $parsed = array(); - $elsestack = array(); - // parse flags - $inelse = false; + // the information about the current active tag + $tag_full = array(); for ($i = 0; $i < strlen($template); $i++) { @@ -482,33 +480,39 @@ class DB_Template if (substr($template, $i, strlen($tag_start)) == $tag_start) { // push the position into the tag stack - array_push($stack, array('pos' => $i, 'depth' => count($stack))); - - //echo "OPEN `$i`: " . print_r($stack, true); + if ($tag_full) + { + array_push($stack, $i); + } + else + { + $tag_full['posi'] = $i; + } } // locate else tags else if (substr($template, $i, strlen($tag_else)) == $tag_else) { - $end = end($stack); - $inelse = true; - array_push($elsestack, array('pos' => $i, $end['depth'])); + if (count($stack) == 0 AND !$tag_full['else']) + { + $tag_full['else'] = $i; + } } // do we have an end tag? else if (substr($template, $i, strlen($tag_end)) == $tag_end) { - // calculate the position of the end tag - $end = $i + strlen($tag_end); - - // find the most recently opened condition - $last = array_pop($stack); + if (count($stack) != 0) + { + array_pop($stack); + continue; + } - // how deep are we nested? - $depth = count($stack); + $tag_full['posf'] = $i; - //echo "CLOSE `$last`: " . print_r($stack, true); + // calculate the position of the end tag + $end = $tag_full['posf'] + strlen($tag_end); // get the full conditional - $fullspread = substr($template, $last['pos'], $end - $last['pos']); + $fullspread = substr($template, $tag_full['posi'], $end - $last['posf']); // remove the beginning tag $conditional = substr($fullspread, strlen($tag_start)); @@ -525,42 +529,34 @@ class DB_Template // 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[pos]"] = array( - 'raw' => $fullspread, - 'parsed' => '' - ); + // parsed expression + $parsed[0] = $expression; - // get the latest else stack - //$else = end($elsestack); - - /* - if ($else['pos'] == $last['pos'] AND $else['depth'] == $last['depth']) + // handle the else + if ($tag_full['else']) { - $parsed["$depth"]["$last[pos]"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '") . "'; + $parsed[1] = substr($conditional, 0, strlen($template) - $tag_full['else'] - strlen($conditional)); + $parsed[2] = substr($conditional, strlen($template) - $tag_full['else'] - strlen($conditional)); } else { - $parsed["$depth"]["$last[pos]"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '" : "") . "'; - }*/ - } - } - - // sort so the top-most ones are done first - ksort($parsed); - foreach ($parsed AS $depth => $conditionals) - { - // sort so we do them in order - ksort($conditionals); - foreach ($conditionals AS $block) - { - // replace the old with the new - $template = str_replace($block['raw'], $block['parsed'], $template); + $parsed[1] = $conditional; + $parsed[2] = ''; + } + print_r($parsed); + + // final parsed output + $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "'; + + // replace the conditional + $template = str_replace($fullspread, $parsed, $template); + + // reset the parser + //$i = $tag_full['posi'] + strlen($tag_start) + strlen($tag_start_end); + $tag_full = array(); } } - // hackish way to get end tags working - $template = str_replace($tag_else, '" : "', $template); - + return $template; } } -- 2.43.5