Example #1
0
 /**
  * Gibt das Template aus.
  * @param  string   $view Name der darzustellenden View.
  * @return Template       Diese Instanz zur Methodenverkettung.
  */
 public function render($view = '')
 {
     $this->view = $view;
     $evt = new RenderEvent($this);
     DiamondMVC::instance()->trigger($evt);
     if ($evt->isDefaultPrevented()) {
         return $this;
     }
     $view = $this->controller->getView($view)->read();
     ob_start();
     require_once DIAMONDMVC_ROOT . '/templates/' . $this->name . '/index.php';
     $content = ob_get_contents();
     ob_end_clean();
     // Platzhalter ersetzen.
     foreach ($this->bind as $bind => $to) {
         $content = str_replace('${' . $bind . '}', $to, $content);
     }
     $content = preg_replace('/\\$\\{[^\\}]*\\}/', '', $content);
     // Ressourcen ab <head> anhängen.
     $pos = strpos($content, '</head>');
     $preInject = substr($content, 0, $pos);
     $postInject = substr($content, $pos);
     $inject = $this->meta . $view->getMeta();
     // Inject the AMD modules and their dependencies.
     $scripts = array_merge($this->scripts, $view->getScripts());
     $sheets = array_merge($this->stylesheets, $view->getStylesheets());
     // Assemble list of stylesheets to include.
     foreach ($this->controller->getAllModules() as $modules) {
         foreach ($modules as $module) {
             foreach ($module->getScripts() as $script) {
                 $scripts[] = $script;
             }
             foreach ($module->getStylesheets() as $sheet) {
                 $sheets[] = $sheet;
             }
         }
     }
     // No duplicates
     array_unique($sheets);
     array_unique($scripts);
     $inject .= '<script type="applicaton/json" id="amd-modules">' . json_encode($scripts) . '</script>';
     foreach ($sheets as $sheet) {
         $mime = 'stylesheet';
         if (($index = strpos($sheet, ';')) !== false) {
             $mime = substr($sheet, $index + 1);
             $sheet = substr($sheet, 0, $index);
         }
         $inject .= '<link rel="' . $mime . '" href="' . $sheet . '">';
     }
     $content = $preInject . $inject . $postInject;
     $evt = new Event('render');
     $evt->source = 'template';
     $evt->content = $content;
     DiamondMVC::instance()->trigger($evt);
     if (!$evt->isDefaultPrevented()) {
         echo $evt->content;
     }
     return $this;
 }