*
* @param $basePath The local base path for all templates
* @param $langInstance An instance of LanguageSystem (default)
- * @param $ioInstance An instance of FileIOHandler (default, middleware!)
+ * @param $ioInstance An instance of FileIoHandler (default, middleware!)
* @return $tplInstance An instance of TemplateEngine
* @throws BasePathIsEmptyException If the provided $basePath is empty
* @throws InvalidBasePathStringException If $basePath is no string
* @throws BasePathReadProtectedException If $basePath is
* read-protected
*/
- public final static function createTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIOHandler $ioInstance) {
+ public final static function createTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
// Get a new instance
$tplInstance = new TemplateEngine();
}
// Get configuration instance
- $cfgInstance = $tplInstance->getConfigInstance();
+ $cfgInstance = FrameworkConfiguration::getInstance();
// Set the base path
$tplInstance->setBasePath($basePath);
// Set the language and IO instances
$tplInstance->setLanguageInstance($langInstance);
- $tplInstance->setFileIOInstance($ioInstance);
+ $tplInstance->setFileIoInstance($ioInstance);
// Set template extensions
$tplInstance->setRawTemplateExtension($cfgInstance->readConfig("raw_template_extension"));
* required method
*/
private function loadRawTemplateData ($fqfn) {
- // Debug message
- if ((defined('DEBUG_TEMPLATE')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Template <strong>%s</strong> vom Typ <strong>%s</strong> wird geladen.<br />\n",
- $this->__toString(),
- $template,
- $this->getTemplateType()
- ));
-
// Get a input/output instance from the middleware
- $ioInstance = $this->getFileIOInstance();
+ $ioInstance = $this->getFileIoInstance();
// Validate the instance
if (is_null($ioInstance)) {
// Load the raw template
$rawTemplateData = $ioInstance->loadFileContents($fqfn);
- // Debug message
- if ((defined('DEBUG_TEMPLATE')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%s</strong> Byte Rohdaten geladen.<br />\n",
- $this->__toString(),
- strlen($rawTemplateData)
- ));
-
// Store the template's contents into this class
$this->setRawTemplateData($rawTemplateData);
// Initialize all missing variables
foreach ($variableMatches[3] as $key=>$var) {
// Is the variable name valid?
- if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config")) {
+ // @TODO Find a better way than ignoring our instance variable $this
+ if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config") && ($variableMatches[1][$key] != "this")) {
// Invalid variable name
throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR);
+ } elseif ($variableMatches[1][$key] != "this") {
+ // Try to assign it, empty strings are being ignored
+ $this->assignTemplateVariable($variableMatches[1][$key], $var);
}
-
- // Try to assign it, empty strings are being ignored
- $this->assignTemplateVariable($variableMatches[1][$key], $var);
}
}
}
// 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
+ //* DEBUG: */ echo "name=".$currEntry['name'].", value=<pre>".htmlentities($currEntry['value'])."</pre>\n";
$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
$currVariable = $idx->current();
// Transfer it's name/value combination to the $content array
+ //* DEBUG: */ echo $currVariable['name']."=<pre>".htmlentities($currVariable['value'])."</pre>\n";
$dummy[$currVariable['name']] = $currVariable['value'];
}// END - if
$this->finalizeVariableCompilation();
// Prepare the eval() command for comiling the template
- $eval = sprintf("\$this->setCompiledData(\"%s\");",
+ $eval = sprintf("\$result = \"%s\";",
addslashes($this->getRawTemplateData())
);
+ // This loop does remove the backslashes (\) in PHP parameters
+ // @TODO Make this some nicer...
+ while (strpos($eval, "<?") !== false) {
+ // Get left part before "<?"
+ $evalLeft = substr($eval, 0, strpos($eval, "<?"));
+
+ // Get all from right of "<?"
+ $evalRight = substr($eval, (strpos($eval, "<?") + 2));
+
+ // Is this a full PHP tag?
+ if (substr(strtolower($evalRight), 0, 3) == "php") {
+ // Remove "php" string from full PHP tag
+ $evalRight = substr($evalRight, 3);
+ }
+
+ // Cut middle part out and remove escapes
+ $evalMiddle = trim(substr($evalRight, 0, strpos($evalRight, "?>")));
+ $evalMiddle = stripslashes($evalMiddle);
+
+ // Remove the middle part from right one
+ $evalRight = substr($evalRight, (strpos($evalRight, "?>") + 2));
+
+ // And put all together
+ $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight);
+ }
+
+ // Prepare PHP code for eval() command
+ $eval = str_replace(
+ "<%php", "\";",
+ str_replace(
+ "%>", "\$result = \"", $eval
+ )
+ );
+
// Debug message
- if (((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
+ if ((defined('DEBUG_EVAL')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command: <pre><em>%s</em></pre><br />\n",
$this->__toString(),
htmlentities($eval)
));
// Run the constructed command. This will "compile" all variables in
eval($eval);
+
+ // Set the new content
+ $this->setCompiledData($result);
}
/**
// Get the content and set it in the response class
$responseInstance->writeToBody($this->getCompiledData());
}
+
+ /**
+ * Assigns all the application data with template variables
+ *
+ * @param $appInstance A manageable application instance
+ * @return void
+ */
+ public function assignApplicationData (ManageableApplication $appInstance) {
+ // Get long name and assign it
+ $this->assignVariable("app_full_name" , $appInstance->getAppName());
+
+ // Get short name and assign it
+ $this->assignVariable("app_short_name", $appInstance->getAppShortName());
+
+ // Get version number and assign it
+ $this->assignVariable("app_version" , $appInstance->getAppVersion());
+ }
}
// [EOF]