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;
 }
Example #2
0
 protected function _()
 {
     $template = file_get_contents($this->_template);
     // transform some characters
     $template = str_replace("\t", str_repeat("&nbsp;", 15), $template);
     $template = str_replace("\n", "<br/>", $template);
     $tagPattern = "/%([a-z0-9]+)\\:([a-z0-9.]*)\\{*([a-zA-Z0-9:,\\\"']*)\\}*%/";
     $tags = array();
     preg_match_all($tagPattern, $template, $tags, PREG_SET_ORDER);
     // PHASE 1
     foreach ($tags as $key => $tag) {
         $content = '';
         if ($tag[1] == 'env') {
             $content = \t41\Core::htmlEncode(\t41\View::getEnvData($tag[2]));
             unset($tag[$key]);
         }
         if (isset($tag[0])) {
             $template = str_replace($tag[0], $content, $template);
         }
     }
     $this->_document->writeHTML($template);
     // PHASE 2 - other tags
     foreach ($tags as $tag) {
         $content = '';
         switch ($tag[1]) {
             case 'container':
                 $elems = \t41\View::getObjects($tag[2]);
                 if (is_array($elems)) {
                     foreach ($elems as $elem) {
                         $object = $elem[0];
                         $params = $elem[1];
                         if (!is_object($object)) {
                             continue;
                         }
                         /* @var $object t41_Form_Abstract */
                         switch (get_class($object)) {
                             case 't41_Form_List':
                             case 't41_View_Table':
                             case 't41_View_Image':
                             case 't41_View_Component':
                             case 't41_View_Error':
                             case 't41_View_Spacer':
                                 $decorator = \t41\View\Decorator::factory($object, $params);
                                 $decorator->render($this->_document, $this->_width);
                                 break;
                             default:
                                 break;
                         }
                     }
                 }
                 break;
         }
     }
 }
Example #3
0
 /**
  * Parse given tags against current template
  * 
  * @param array $tags
  */
 protected function _parseTags($tags)
 {
     foreach ($tags as $tag) {
         $value = null;
         switch ($tag[1]) {
             case 'var':
                 $keys = explode('.', $tag[2]);
                 $value = $this->_obj->getVariable($keys[0]);
                 if (count($keys) > 1) {
                     $value = $value[$keys[1]];
                 }
                 break;
             case 'env':
                 $value = View::getEnvData($tag[2]);
                 break;
             case 'container':
                 if (($templates = $this->_obj->getSubtemplates($tag[2])) !== false) {
                     $value = '';
                     foreach ($templates as $template) {
                         $deco = Decorator::factory($template);
                         $value .= $deco->render();
                     }
                 }
                 break;
             default:
                 // obj:
                 $tmp = explode('.', $tag[2]);
                 $obj = $this->_obj->getVariable($tmp[0]);
                 if ($obj instanceof MediaObject && isset($tmp[1])) {
                     // meta properties handling
                     switch ($tmp[1]) {
                         case '_base64':
                             $value = sprintf('data:%s;base64,%s', $obj->getMime(), base64_encode($obj->loadBlob('media')));
                             break;
                         case '_icon':
                             $value = 'file-' . $obj->getExtension();
                             break;
                         case '_size':
                             $value = MediaElement::getDisplaySize($obj->getSize());
                             break;
                         case '_url':
                             $value = MediaElement::getDownloadUrl($obj->getUri());
                         default:
                             break;
                     }
                 }
                 if (!$value) {
                     if ($obj instanceof BaseObject) {
                         $value = isset($tmp[1]) && $tmp[1] == ObjectUri::IDENTIFIER ? $obj->getIdentifier() : $obj->getProperty($tmp[1]);
                         $value = $value instanceof AbstractProperty ? $value->getDisplayValue() : $value;
                     } else {
                         $value = Core::$env == Core::ENV_DEV ? sprintf("Can't substitute any value to '%s'", $tag[0]) : null;
                     }
                 }
                 break;
         }
         if ($value instanceof ViewObject) {
             $deco = Decorator::factory($value);
             $value = $deco->render();
         } else {
             //$value = $this->_escape($value);
         }
         $this->_template = str_replace($tag[0], $value, $this->_template);
     }
 }