From 83f859ecf9065dbf00dd03f4f15361d6f6674d8a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 19 Mar 2005 03:39:10 +0000 Subject: [PATCH] Back to tokenization. The stack method WORKS, but we need to figure out why parsing it does not. *sigh* so complicated... wait... I think I know... HOLD ON :). --- template.php | 144 ++++++++++++++++++++++++--------------------------- 1 file changed, 67 insertions(+), 77 deletions(-) diff --git a/template.php b/template.php index 24f6f1f..9a58c5d 100644 --- a/template.php +++ b/template.php @@ -400,91 +400,81 @@ class DB_Template $tag_start_end = '\">'; $tag_else = ''; $tag_end = ''; - - $data = preg_replace('##', '" . iff(\\1,"', $template); - $data = str_replace('', '","', $data); - $data = str_replace('', '") . "', $data); - - return $data; - - /*$stack = array(); + + $stack = array(); for ($i = 0; $i < strlen($template); $i++) { // get the current character $char = $template[ $i ]; - // is the char equal to the first character of the expression? - if ($char == substr($tag_start, 0, 1) OR $char == substr($tag_end, 0, 1)) + // we've found ourselves a conditional! + if (substr($template, $i, strlen($tag_start)) == $tag_start) { - // we've found ourselves a condition! - if (substr($template, $i, strlen($tag_start)) == $tag_start) - { - // where do we end the condition - $end = strpos($template, $tag_start_end, $i); - $end += strlen($tag_start_end); - //echo $template[ $end - 2 ] . $template[ $end - 1 ] . $template[ $end ] . $template[ $end + 1 ] . $template[ $end + 2 ] . " (" . $template[ $end ] . ") "; - //echo " { " . $template[ $end - 1 ] . " } "; - //echo " >>>>>> " . $end . " >>>>>> "; - - // extract the condition - $condition = substr($template, $i, $end - $i); - - //echo "||||||" . $_isso->sanitize($condition) . "||||||"; - - // complete the open - array_push($stack, $i); - - //print_r($stack); - } - // do we have an end? - // make sure that it's the whole tag - else if (substr($template, $i, strlen($tag_end)) == $tag_end) - { - // calculate the end of the tag - $end = $i + strlen($tag_end); - - //echo ">>>>> $end <<<<<<"; - - //print_r($stack); - - // find the most recently opened condition - $last = array_pop($stack); - - // get the full condition - $fullspread = substr($template, $last, $end - $last); - - // remove the beginning tag - $conditional = substr($fullspread, strlen($tag_start)); - - // 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)); - - //echo ">>>>>>> " . $_isso->sanitize($conditional) . " >>>>>>>"; - - // place it in iff() - $parsed = '" . iff(' . stripslashes($expression) . ',"' . $conditional . '","") . "'; - - echo "<<<< " . $_isso->sanitize($parsed) . " >>>> "; - - // replace it back into the template - $template = substr_replace($template, $parsed, $i, $i - $end + strlen($tag_end)); - - echo ">>>>>>>>>>> $template <<<<<<<<<<"; - exit; - //echo "<<<<<<<<<<<<<<<<< $conditional >>>>>>>>>>>"; - } + // where do we end the condition + $end = strpos($template, $tag_start_end, $i); + $end += strlen($tag_start_end); + //echo $template[ $end - 2 ] . $template[ $end - 1 ] . $template[ $end ] . $template[ $end + 1 ] . $template[ $end + 2 ] . " (" . $template[ $end ] . ") "; + //echo " { " . $template[ $end - 1 ] . " } "; + //echo " >>>>>> " . $end . " >>>>>> "; + + // extract the condition + $condition = substr($template, $i, $end - $i); + + //echo "||||||" . $_isso->sanitize($condition) . "||||||"; + + // complete the open + array_push($stack, $i); + + echo "OPEN `$i`: " . print_r($stack, true); } - }*/ + // do we have an end? + // make sure that it's the whole tag + else if (substr($template, $i, strlen($tag_end)) == $tag_end) + { + // calculate the end of the tag + $end = $i + strlen($tag_end); + + //echo ">>>>> $end <<<<<<"; + + // find the most recently opened condition + $last = array_pop($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)); + + // 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)); + + //echo ">>>>>>> " . $_isso->sanitize($conditional) . " >>>>>>>"; + + // place it in iff() + $parsed = '" . iff(' . stripslashes($expression) . ',"' . $conditional . '","") . "'; + + //echo "<<<< " . $_isso->sanitize($parsed) . " >>>> "; + + // replace it back into the template + //$template = substr_replace($template, $parsed, $i, $i - $end + strlen($tag_end)); + + //echo ">>>>>>>>>>> $template <<<<<<<<<<"; + //exit; + //echo "<<<<<<<<<<<<<<<<< $conditional >>>>>>>>>>>"; + } + } return ''; //return $template; // ------------------------------ -- 2.43.5