support for view helpers added
[mailer.git] / inc / classes / main / template / class_TemplateEngine.php
index 74542634f7466235ec2e3b87e8a5547df81c6ce3..2fff655b7c4b7a98362a024775e769953925f320 100644 (file)
@@ -108,9 +108,15 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
         */
        private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
 
+       /**
+        * Loaded helpers
+        */
+       private $helpers = array();
+
        // Exception codes for the template engine
        const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0xa00;
-       const TEMPLATE_CONTAINS_INVALID_VAR_EXCEPTION = 0xa01;
+       const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0xa01;
+       const EXCEPTION_INVALID_VIEW_HELPER           = 0xa02;
 
        /**
         * Private constructor
@@ -628,7 +634,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                                // Is the variable name valid?
                                if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config")) {
                                        // Invalid variable name
-                                       throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::TEMPLATE_CONTAINS_INVALID_VAR_EXCEPTION);
+                                       throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR);
                                }
 
                                // Try to assign it, empty strings are being ignored
@@ -1034,6 +1040,43 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        break;
                }
        }
+
+       /**
+        * Loads a given view helper (by name)
+        *
+        * @param       $helperName     The helper's name
+        * @return      void
+        * @throws      ViewHelperNotFoundException     If the given view helper was not found
+        */
+       protected function loadViewHelper ($helperName) {
+               // Make first character upper case, rest low
+               $helperName = ucfirst($helperName);
+
+               // Is this view helper loaded?
+               if (!isset($this->helpers[$helperName])) {
+                       // Create a class name
+                       $className = "{$helperName}ViewHelper";
+
+                       // Does this class exists?
+                       if (!class_exists($className)) {
+                               // Abort here!
+                               throw new ViewHelperNotFoundException(array($this, $helperName), self::EXCEPTION_INVALID_VIEW_HELPER);
+                       }
+
+                       // Generate new instance
+                       $eval = sprintf("\$this->helpers[%s] = %s::create%s();",
+                               $helperName,
+                               $className,
+                               $className
+                       );
+
+                       // Run the code
+                       @eval($eval);
+               }
+
+               // Return the requested instance
+               return $this->helpers[$helperName];
+       }
 }
 
 // [EOF]