Example #1
0
 /**
  * Echo out parsed template. Main and only supported template engine is Smarty.
  * If you want another template engine, than in your BaseController overwrite this method.
  * @param string|array $template
  * @param string $layout
  */
 public function render($template = null, $layout = null)
 {
     if ($this->view instanceof \Smarty) {
         $name = $this->name;
         if ($template === null or !is_array($template) and strpos($template, ':') !== false) {
             // neni to nazov sablony ale Controller::action
             if (strpos($template, ':') !== false) {
                 $name = $template;
             }
             $dir = $this->view->template_dir;
             $file = str_replace(':', '.', $name);
             if (file_exists($dir . "{$file}.tpl")) {
                 $this->view->assign('template', $file);
                 if ($layout !== null) {
                     // TODO: chek if layout file exists, but not now, I'm too lazy...
                     $this->view->display($layout);
                 } else {
                     $this->view->display('@layout.tpl');
                 }
             } else {
                 throw new \Exception("Template {$file} not found.");
             }
         } else {
             // sikovny kus kodu v Stupid Frameworku :D
             if ($this->isAjax()) {
                 $payload = (object) null;
                 if (is_array($template)) {
                     $snippetIds = array();
                     foreach ($template as $t) {
                         $snippetId = strtolower(implode('', array_slice(array_reverse(explode('.', $t)), 1, 1)));
                         $payload->snippets[$snippetId] = $this->smarty->fetch($t);
                     }
                 } else {
                     // finta: posledne slovo pred .tpl je ID elementu ktory budeme ajaxom aktualizovat
                     $snippetId = strtolower(implode('', array_slice(array_reverse(explode('.', $template)), 1, 1)));
                     $payload->snippets[$snippetId] = $this->smarty->fetch($template);
                 }
                 $this->sendPayload($payload);
             } else {
                 $this->smarty->display($template);
             }
         }
     } else {
         throw new \Exception("Other template engines are not supported now.");
     }
 }