From: Roland Häder Date: Mon, 21 Apr 2008 11:28:16 +0000 (+0000) Subject: Template engine does now insert all variables (work-around) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=850b08879a6e4287b9a81d70a44a973a5da2ee69;p=shipsimu.git Template engine does now insert all variables (work-around) --- diff --git a/inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php b/inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php index 568fa91..bfb096e 100644 --- a/inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php +++ b/inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php @@ -25,17 +25,17 @@ class UnexpectedTemplateTypeException extends FrameworkException { /** * The constructor * - * @param $class An array holding our informations + * @param $msgArray An array holding our informations * @param $code Code number for the exception * @return void */ - public function __construct (BaseFrameworkSystem $class, $code) { + public function __construct (array $msgArray, $code) { // Add a message around the missing class $message = sprintf("[%s:%d] Der Template-Typ %s entspricht nicht dem erwartetem Template-Typ %s.", - $class[0]->__toString(), + $msgArray[0]->__toString(), $this->getLine(), - $class[1], - $class[2] + $msgArray[1], + $msgArray[2] ); // Call parent constructor diff --git a/inc/classes/main/template/class_TemplateEngine.php b/inc/classes/main/template/class_TemplateEngine.php index 16fbd3c..6e09d89 100644 --- a/inc/classes/main/template/class_TemplateEngine.php +++ b/inc/classes/main/template/class_TemplateEngine.php @@ -877,6 +877,32 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate } } + /** + * Finalizes the compilation of all template variables + * + * @return void + */ + private function finalizeVariableCompilation () { + // Get the content + $content = $this->getRawTemplateData(); + + // Walk through all variables + for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) { + + // Get current entry + $currEntry = $idx->current(); + + // Replace all [$var] or {?$var?} with the content + $content = str_replace("\$content[".$currEntry['name']."]", $currEntry['value'], $content); + $content = str_replace("[".$currEntry['name']."]", $currEntry['value'], $content); + $content = str_replace("{?".$currEntry['name']."?}", $currEntry['value'], $content); + + } // END - for + + // Set the content back + $this->setRawTemplateData($content); + } + /** * Getter for raw template data * @@ -950,12 +976,16 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Iterate through all variables for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) { + // Get current variable from the stack $currVariable = $idx->current(); // Transfer it's name/value combination to the $content array $dummy[$currVariable['name']] = $currVariable['value']; - } + + }// END - if + + // Set the new variable (don't remove the second dollar !) $$validVar = $dummy; // Prepare all configuration variables @@ -965,6 +995,9 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate unset($idx); unset($currVariable); + // Finalize the compilation of template variables + $this->finalizeVariableCompilation(); + // Prepare the eval() command for comiling the template $eval = sprintf("\$this->setCompiledData(\"%s\");", addslashes($this->getRawTemplateData()) @@ -994,7 +1027,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate if ($this->getTemplateType() != $this->getConfigInstance()->readConfig("code_template_type")) { // Abort here throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->readConfig("code_template_type")), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); - } + } // END - if // Get the raw data. $rawData = $this->getRawTemplateData(); @@ -1014,7 +1047,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // // The main analysis $this->analyzeTemplate($templateMatches); - $this->debugInstance(); // Compile raw template data $this->compileRawTemplateData($templateMatches); @@ -1024,9 +1056,14 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Are some raw templates found and loaded? if (count($this->rawTemplates) > 0) { + // Insert all raw templates $this->insertRawTemplates(); - } + + // Remove the raw template content as well + $this->setRawTemplateData(""); + + } // END - if } // END - if($templateMatches ... }