Example #1
0
 /**
  * @param ViewModel $model
  * @return string
  */
 public function __invoke(ViewModel $model)
 {
     foreach ($model as $k => $v) {
         $v instanceof ViewModel && $model->set($k, $this($v));
     }
     if ($template = $this->template($model->path())) {
         $model->template($template);
     }
     if (!$model->path()) {
         throw new RuntimeException('View model path not found: ' . get_class($model));
     }
     $model instanceof Plugin && !$model->viewManager() && $model->setViewManager($this->viewManager());
     $render = Closure::bind(function () {
         /** @var ViewModel $this */
         extract($this->assigned());
         ob_start();
         try {
             include $this->path();
             return ob_get_clean();
         } catch (Exception $exception) {
             ob_get_clean();
             throw $exception;
         }
     }, $model);
     return $render();
 }
Example #2
0
 /**
  * @param string $template
  * @param array $vars
  * @return Model
  */
 public function view($template = null, array $vars = [])
 {
     $this->model($vars);
     $template && $this->model->template($template);
     return $this->model;
 }