[phplib-dev] cvs commit From: uw (phplib-dev <email protected>)
Date: 10/09/00

From: uw
Date: Mon Oct 9 17:58:08 2000
Modified files:
      php-lib/php/ext/integratedtemplate.inc

Log message:
- removed a bug with nested template blocks

Index: php-lib/php/ext/integratedtemplate.inc
diff -u php-lib/php/ext/integratedtemplate.inc:1.2 php-lib/php/ext/integratedtemplate.inc:1.3
--- php-lib/php/ext/integratedtemplate.inc:1.2 Tue Sep 26 10:01:52 2000
+++ php-lib/php/ext/integratedtemplate.inc Mon Oct 9 17:57:36 2000
@@ -28,7 +28,7 @@
 * $tpl->get();
 *
 *  <email protected> Ulf Wendel <uw <email protected>>
-*  <email protected> 1.0 22/09/00
+*  <email protected> 1.1 28/09/00
 *  <email protected> public
 */
 class IntegratedTemplate {
@@ -58,6 +58,12 @@
         var $haltOnError = false;
         
         /**
+ * Clear cache on get()?
+ *  <email protected> boolean
+ */
+ var $clearCache = false;
+
+ /**
         * First character of a variable placeholder ( _{_VARIABLE} ).
         *  <email protected> string
         *  <email protected> public
@@ -126,25 +132,32 @@
         *  <email protected> findBlocks()
         */
         var $blocklist = array();
+
+ /**
+ * Array with the parsed content of a block.
+ *
+ *  <email protected> array
+ */
+ var $blockdata = array();
         
         /**
         * Array of variables in a block.
         *  <email protected> array
         */
         var $blockvariables = array();
-
- /**
- * Array of cached data of a block.
- *  <email protected> array
- */
- var $blockdata = array();
-
+
         /**
         * Array of inner blocks of a block.
         *  <email protected> array
         */
         var $blockinner = array();
         
+ /**
+ * Future versions will use this...
+ *  <email protected> array
+ */
+ var $blocktypes = array();
+
         /**
         * Variable cache.
         *
@@ -153,21 +166,26 @@
         * Disadvantage: might take some more memory
         *
         *  <email protected> array
- *  <email protected> setVariable()
+ *  <email protected> setVariable(), $clearCacheOnParse
         */
         var $variableCache = array();
         
         /**
+ *  <email protected> boolean
+ */
+ var $clearCacheOnParse = true;
+
+ /**
         * Controls the handling of unknown variables, default is remove.
         *  <email protected> boolean
         */
         var $removeUnknownVariables = true;
         
         /**
- * Controls the handling of empty blocks, default is keep.
+ * Controls the handling of empty blocks, default is remove.
         *  <email protected> boolean
         */
- var $removeEmptyBlocks = false;
+ var $removeEmptyBlocks = true;
         
         /**
         * Root directory for all file operations.
@@ -184,6 +202,12 @@
         var $flagBlocktrouble = false;
         
         /**
+ * Flag indicating that the global block was parsed.
+ *  <email protected> boolean
+ */
+ var $flagGlobalParsed = false;
+
+ /**
         * Builds some complex regular expressions and optinally sets the file root directory.
         *
         * Make sure that you call this constructor if you derive your template
@@ -212,11 +236,6 @@
         /**
         * Returns a block with all replacements done.
         *
- * Do not use getReplacedBlock() directy. getReplacedBlock() was
- * written to loop recursively through the internal blocklist.
- * This function does not only do the replacement of variables itself
- * but replaces unused variables and cares on "global" variables.
- *
         *  <email protected> string name of the block
         *  <email protected> string
         *  <email protected> public
@@ -224,108 +243,113 @@
         */
         function get($block = "__global__") {
 
+ if ("__global__" == $block && !$this->flagGlobalParsed)
+ $this->parse("__global__");
+
                 if (!isset($this->blocklist[$block])) {
                         $this->halt("The block '$block' was not found in the template.", __FILE__, __LINE__);
- return "";
+ return true;
                 }
                 
- if ("__global__" == $block)
- $this->addGlobalVariablesToBlocklist();
+ if ($this->clearCache) {
+
+ $data = (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";
+ unset($this->blockdata[$block]);
+ return $data;
+
+ } else {
+
+ return (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";
+
+ }
 
- $result = $this->getReplacedBlock($block);
-
- if ($this->removeUnkownVariables)
- $result = preg_replace($this->variablesRegExp, "", $result);
-
- return $result;
         } // end func get()
-
- /**
- * Returns a block with alll replacements done.
- *
- *  <email protected> string name of the block
- *  <email protected> string content of the block with all replacements done
- *  <email protected> get()
- */
- function getReplacedBlock($block = "__global__") {
-
- $result = "";
- $result.= $this->replace( $this->blocklist[$block], $this->blockdata[$block]);
-
- if (isset($this->blockinner[$block])) {
-
- reset($this->blockinner[$block]);
- while (list($k, $innerblock)=each($this->blockinner[$block])) {
- $innerresult = $this->getReplacedBlock($innerblock);
- $result = str_replace($this->openingDelimiter."__".$innerblock."__".$this->closingDelimiter, $innerresult, $result);
- }
-
- }
-
- return $result;
- } // end func getReplacedBlock
-
- /**
- * Does the variable replacement.
- *
- * I had a long discussion on how the replacement should be done.
- * preg_replace() used with arrays of less than 50 elements is
- * faster as than str_replace used with a loop. But strtr() is the
- * notably fastest of all of them...
- *
- *  <email protected> string string where the replacement is applied to
- *  <email protected> array array of values to be replaced
- *  <email protected> get()
- */
- function replace($string, $data) {
-
- if (0==count($data))
- return ($this->removeEmptyBlocks) ? "" : $string;
-
- $target = "";
                 
- reset($data);
- while (list($k, $variables)=each($data))
- $target.= preg_replace($variables["var"], $variables["val"], $string);
-
- return $target;
- } // end func replace
-
         /**
         * Parses the given block.
- *
- * Parse does not mean replace. Parse means a copy from
- * a global cache to a block cache. This is different to
- * the phplib templates.
         *
         *  <email protected> string name of the block to be parsed
- *  <email protected> boolean false on failure otherwise true
         *  <email protected> public
         *  <email protected> parseCurrentBlock()
         */
- function parse($block = "__global__") {
-
+ function parse($block = "__global__", $flag_recursion = false) {
+
                 if (!isset($this->blocklist[$block])) {
                         $this->halt("The block '$block' was not found in the template.", __FILE__, __LINE__);
                         return false;
                 }
 
- $index = count($this->blockdata[$block]);
+ if ("__global__" == $block)
+ $this->flagGlobalParsed = true;
+
+ $regs = array();
+ $values = array();
+
+ if ($this->clearCacheOnParse) {
+
+ reset($this->variableCache);
+ while (list($name, $value)=each($this->variableCache)) {
+ $regs[] = "@".$this->openingDelimiter.$name.$this->closingDelimiter."@";
+ $values[] = $value;
+ }
+ $this->variableCache = array();
                 
- reset($this->blockvariables[$block]);
- while (list($k, $allowedvar)=each($this->blockvariables[$block])) {
+ } else {
                 
- if (isset($this->variableCache[$allowedvar])) {
- $this->blockdata[$block][$index]["var"][] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
- $this->blockdata[$block][$index]["val"][] = $this->variableCache[$allowedvar];
- unset($this->variableCache[$allowedvar]);
- }
+ reset($this->blockvariables[$block]);
+ while (list($k, $allowedvar)=each($this->blockvariables[$block])) {
+
+ if (isset($this->variableCache[$allowedvar])) {
+ $regs[] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
+ $values[] = $this->variableCache[$allowedvar];
+ unset($this->variableCache[$allowedvar]);
+ }
+
+ }
                         
                 }
+
+ $outer = preg_replace($regs, $values, $this->blocklist[$block]);
+ $empty = (0==count($values)) ? true : false;
+
+ if (isset($this->blockinner[$block])) {
                 
- return true;
+ reset($this->blockinner[$block]);
+ while (list($k, $innerblock)=each($this->blockinner[$block])) {
+
+ $this->parse($innerblock, true);
+ if (""!=$this->blockdata[$innerblock])
+ $empty = false;
+
+ $placeholder = $this->openingDelimiter."__".$innerblock."__".$this->closingDelimiter;
+ $outer = str_replace($placeholder, $this->blockdata[$innerblock], $outer);
+ $this->blockdata[$innerblock] = "";
+ }
+ }
+
+ if ($this->removeUnknownVariables)
+ $outer = preg_replace($this->variablesRegExp, "", $outer);
+
+ if ($empty) {
+
+ if (!$this->removeEmptyBlocks)
+ $this->blockdata[$block].= $outer;
+
+ } else {
+
+ $this->blockdata[$block].= $outer;
+
+ }
+
+ return $empty;
         } // end func parse
         
+ function j($bool) {
+ if ($bool)
+ print "ja";
+ else
+ print "nein";
+ }
         /**
         * Parses the current block
         *  <email protected> parse(), setCurrentBlock(), $currentBlock
@@ -344,6 +368,7 @@
         *
         *  <email protected> mixed string with the variable name or an array %variables["varname"] = "value"
         *  <email protected> string value of the variable or empty if $variable is an array.
+ *  <email protected> string prefix for variable names
         *  <email protected> public
         */
         function setVariable($variable, $value="") {
@@ -418,8 +443,11 @@
                 $this->blockvariables = array();
                 $this->blockinner = array();
                 $this->blockdata = array();
+ $this->blocklookup = array();
+ $this->blocktypes = array();
                 
                 $this->flagBlocktrouble = false;
+ $this->flagGlobalParsed = false;
                 
         } // end func free
         
@@ -464,7 +492,7 @@
         *  <email protected> boolean false on failure, otherwise true
         *  <email protected> $template, setTemplate()
         */
- function loadTemplatefile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = false) {
+ function loadTemplatefile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) {
         
                 $template = $this->getfile($filename);
                 
@@ -483,7 +511,7 @@
         */
         function setRoot($root) {
                 
- if ("/"!= substr($root, -1))
+ if (""!=$root && "/"!= substr($root, -1))
                         $root.="/";
                 
                 $this->fileRoot = $root;
@@ -504,22 +532,26 @@
         } // end func buildBlockvariablelist
         
         /**
- * Adds all variables for the "global" block to the global blocklist entry.
+ * Returns a list of all
         */
- function addGlobalVariablesToBlocklist() {
+ function getGlobalvariables() {
 
+ $regs = array();
+ $values = array();
+
                 reset($this->blockvariables["__global__"]);
                 while (list($k, $allowedvar)=each($this->blockvariables["__global__"])) {
                         
                         if (isset($this->variableCache[$allowedvar])) {
- $this->blockdata["__global__"][0]["var"][] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
- $this->blockdata["__global__"][0]["val"][] = $this->variableCache[$allowedvar];
+ $regs[] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
+ $values[] = $this->variableCache[$allowedvar];
                                 unset($this->variableCache[$allowedvar]);
                         }
                         
                 }
                 
- } // end func addGlobalVariablesToBlocklist
+ return array($regs, $values);
+ } // end func getGlobalvariables
 
         /**
         * Recusively builds a list of all blocks within the template.
@@ -539,15 +571,14 @@
                         
                                 $blockname = $match[1];
                                 $blockcontent = $match[2];
-
+
                                 if (isset($this->blocklist[$blockname])) {
                                         $this->halt("The name of a block must be unique within a template. Found '$blockname' twice. Unpredictable results may appear.", __FILE__, __LINE__);
                                         $this->flagBlocktrouble = true;
                                 }
 
- $this->blocklist[$blockname] = $blockcontent;
- $this->blockdata[$blockname] = array();
- $this->blockvariables[$blockname] = array();
+ $this->blocklist[$blockname] = $blockcontent;
+ $this->blockdata[$blockname] = "";
 
                                 $blocklist[] = $blockname;
                                 
@@ -565,6 +596,7 @@
                                                                                                                                                                                                                                 $this->blocklist[$blockname]
                                                                                                                                                                                                                         );
                                         $this->blockinner[$blockname][] = $name;
+ $this->blockparents[$name] = $blockname;
                                         
                                 }
                                 
@@ -624,4 +656,4 @@
         } // end func halt
         
 } // end class IntegratedTemplate
-?>
+?>
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>