Example #1
0
 protected function _render()
 {
     $template = file_get_contents($this->_template);
     $tagPattern = "/%([a-z0-9]+)\\:([a-z0-9.]*)\\{*([a-zA-Z0-9:,\\\"']*)\\}*%/";
     $tagPattern = "/%([a-z0-9]+)\\:([a-z0-9_.]*)\\{*([a-zA-Z0-9:_,\\\"']*)\\}*%/";
     $tags = array();
     preg_match_all($tagPattern, $template, $tags, PREG_SET_ORDER);
     // PHASE 1: analyse et parsing of content-generating tags
     foreach ($tags as $tag) {
         $content = false;
         switch ($tag[1]) {
             case 'helper':
                 $tmp = explode('.', $tag[2]);
                 $class = sprintf('%s\\View\\Web\\%s', $tmp[0], ucfirst($tmp[1]));
                 try {
                     $helper = new $class();
                     $content = $helper->render();
                 } catch (Exception $e) {
                     if (Core::$env == Core::ENV_DEV) {
                         $content = $e->getMessage();
                     }
                 }
                 break;
             case 'container':
                 $elems = View::getObjects($tag[2]);
                 if (is_array($elems)) {
                     foreach ($elems as $elem) {
                         $object = $elem[0];
                         $params = $elem[1];
                         if (!is_object($object)) {
                             continue;
                         }
                         // look for a custom decorator
                         $decorator = View\Decorator::factory($object, $params);
                         $content .= $decorator->render();
                     }
                 }
                 break;
         }
         if ($content !== false) {
             $template = str_replace($tag[0], $content, $template);
         }
     }
     preg_match_all($tagPattern, $template, $tags, PREG_SET_ORDER);
     // PHASE 2: analyze & parsing of other tags (components linking & env variables)
     foreach ($tags as $tag) {
         $content = null;
         switch ($tag[1]) {
             case 'components':
                 $content = $this->_renderComponents($tag[2], $tag[3]);
                 break;
             case 'env':
             case 'var':
                 $content = View::getEnvData($tag[2]);
                 break;
             case 'obj':
                 // obj:
                 $tmp = explode('.', $tag[2]);
                 $obj = View::getEnvData($tmp[0]);
                 if ($obj instanceof MediaObject && isset($tmp[1])) {
                     // meta properties handling
                     switch ($tmp[1]) {
                         case '_base64':
                             $content = sprintf('data:%s;base64,%s', $obj->getMime(), base64_encode($obj->loadBlob('media')));
                             break;
                         case '_icon':
                             $content = 'file-' . $obj->getExtension();
                             break;
                         case '_size':
                             $content = MediaElement::getDisplaySize($obj->getSize());
                             break;
                         case '_url':
                             $content = MediaElement::getDownloadUrl($obj->getUri());
                             break;
                         default:
                             break;
                     }
                 }
                 if (!$content) {
                     if ($obj instanceof BaseObject) {
                         $content = isset($tmp[1]) && $tmp[1] == ObjectUri::IDENTIFIER ? $obj->getIdentifier() : $obj->getProperty($tmp[1]);
                         $content = $content instanceof AbstractProperty ? $content->getDisplayValue() : $content;
                     } else {
                         $content = Core::$env == Core::ENV_DEV ? sprintf("Can't substitute any value to '%s'", $tag[0]) : null;
                     }
                 }
                 break;
         }
         $template = str_replace($tag[0], $content, $template);
     }
     // PHASE 3: actions attachment
     $this->actionAttach();
     // PHASE 4: events attachment
     $template = str_replace('</body>', $this->eventAttach() . '</body>', $template);
     // PHASE 5: display logged errors in dev mode
     if (Core::$env == Core::ENV_DEV) {
         $errors = View::getErrors();
         if (count($errors) > 0) {
             $str = "\n";
             foreach ($errors as $errorCode => $errorbloc) {
                 $str .= $errorCode . "\n";
                 foreach ($errorbloc as $error) {
                     $str .= "\t" . $error[0] . "\n";
                 }
             }
             $template = str_replace('</body>', '</body><!--' . $str . ' -->', $template);
         }
     }
     return $template;
 }