Esempio n. 1
0
 /**
  * Renders the template by page with the additional data
  * 
  * @param Page|string $template
  * @param array $data
  * @param boolean $return
  * @return string
  */
 public function render($template, $data = [], $return = true)
 {
     if ($template instanceof Page) {
         $page = $template;
         $file = $page->templateFile();
         $data = $this->data($page, $data);
     } else {
         $file = $template;
         $data = $this->data(null, $data);
     }
     // check for an existing template
     if (!file_exists($file)) {
         throw new Exception('The template could not be found');
     }
     // merge and register the template data globally
     tpl::$data = array_merge(tpl::$data, $data);
     // load the template
     return tpl::load($file, null, $return);
 }
Esempio n. 2
0
 public function run($path = '/')
 {
     if ($this->kirby->option('patterns.lock') && !$this->kirby->site()->user()) {
         go($this->kirby->option('error'));
     }
     // error handling
     $whoops = new \Whoops\Run();
     $whoops->pushHandler(function ($e) {
         throw $e;
         return \Whoops\Handler\Handler::QUIT;
     });
     $whoops->register();
     tpl::$data = ['site' => $this->kirby->site(), 'pages' => $this->kirby->site()->children(), 'page' => $this->kirby->site()->find($this->kirby->option('home')), 'lab' => $this];
     $router = new Router();
     $router->register([['pattern' => '/', 'action' => function () {
         $readme = $this->root() . DS . 'readme.md';
         if (!is_dir($this->root())) {
             $modal = $this->view('modals/folder');
         } else {
             $modal = null;
         }
         if (is_file($readme)) {
             $markdown = kirbytext(f::read($readme));
         } else {
             $markdown = null;
         }
         return $this->view('layouts/main', ['title' => $this->title(), 'menu' => $this->menu(), 'content' => $this->view('views/dashboard', ['markdown' => $markdown]), 'modal' => $modal]);
     }], ['pattern' => 'assets/(:any)', 'action' => function ($file) {
         switch ($file) {
             case 'index.js':
             case 'index.min.js':
                 $mime = 'text/javascript';
                 break;
             case 'index.css':
             case 'index.min.css':
                 $mime = 'text/css';
                 break;
             default:
                 return new Response('Not found', 'text/html', 404);
                 break;
         }
         // build the root for the file
         $file = dirname(__DIR__) . DS . 'assets/dist/' . $file;
         return new Response(f::read($file), $mime);
     }], ['pattern' => '(:all)/preview', 'action' => function ($path) {
         lab::$mode = 'preview';
         $pattern = new Pattern($path);
         $config = $pattern->config();
         try {
             $html = $pattern->render();
         } catch (Exception $e) {
             $html = '';
         }
         return $this->view('views/preview', ['pattern' => $pattern, 'html' => $html, 'background' => a::get($config, 'background', $this->kirby->option('patterns.preview.background')), 'css' => $this->kirby->option('patterns.preview.css', 'assets/css/index.css'), 'js' => $this->kirby->option('patterns.preview.js', 'assets/js/index.js')]);
     }], ['pattern' => '(:all)', 'action' => function ($path) {
         $pattern = new Pattern($path);
         $file = null;
         if (!$pattern->exists()) {
             $filename = basename($path);
             $path = dirname($path);
             if ($path == '.') {
                 $preview = $this->view('previews/error', ['error' => 'The pattern could not be found']);
             } else {
                 $pattern = new Pattern($path);
                 $file = $pattern->files()->get($filename);
                 if ($file) {
                     $preview = $this->preview($pattern, $file);
                 } else {
                     $preview = $this->view('previews/error', ['error' => 'The file could not be found']);
                 }
             }
         } else {
             if ($file = $pattern->files()->get($pattern->name() . '.html.php')) {
                 go($pattern->url() . '/' . $file->filename());
             } else {
                 if ($file = $pattern->files()->first()) {
                     go($pattern->url() . '/' . $file->filename());
                 } else {
                     $preview = $this->view('previews/empty');
                 }
             }
         }
         if ($pattern->isHidden()) {
             go($this->url());
         }
         return $this->view('layouts/main', ['title' => $this->title() . ' / ' . $pattern->title(), 'menu' => $this->menu(null, $path), 'content' => $this->view('views/pattern', ['preview' => $preview, 'info' => $this->view('snippets/info', ['pattern' => $pattern, 'file' => $file])])]);
     }]]);
     if ($route = $router->run($path ? $path : '/')) {
         return new Response(call($route->action(), $route->arguments()));
     } else {
         go('error');
     }
 }
Esempio n. 3
0
 /**
  * Template configuration
  *
  * @param Page $page
  * @param array $data
  * @return string
  */
 public function template(Page $page, $data = array())
 {
     // apply the basic template vars
     tpl::$data = array_merge(tpl::$data, array('kirby' => $this, 'site' => $this->site(), 'pages' => $this->site()->children(), 'page' => $page), $page->templateData(), $data, $this->controller($page, $data));
     return tpl::load($page->templateFile());
 }
Esempio n. 4
0
 /**
  * Template configuration
  *
  * @param Page $page
  * @param array $data
  * @return string
  */
 public function template(Page $page, $data = array())
 {
     // apply the basic template vars
     tpl::$data = array_merge(tpl::$data, array('kirby' => $this, 'site' => $this->site(), 'pages' => $this->site()->children(), 'page' => $page), $page->templateData(), $data, $this->controller($page, $data));
     if (!file_exists($page->templateFile())) {
         throw new Exception('The default template could not be found');
     }
     return tpl::load($page->templateFile());
 }
Esempio n. 5
0
 /**
  * Template configuration
  *
  * @param Page $page
  * @param array $data
  * @return string
  */
 public function template(Page $page, $data = array())
 {
     // set the timezone for all date functions
     date_default_timezone_set($this->options['timezone']);
     // load all language variables
     $this->localize();
     // load all extensions
     $this->extensions();
     // load all plugins
     $this->plugins();
     // apply the basic template vars
     tpl::$data = array_merge(array('kirby' => $this, 'site' => $this->site(), 'pages' => $this->site()->children(), 'page' => $page), $data, $this->controller($page, $data));
     return tpl::load($page->templateFile());
 }
Esempio n. 6
0
 /**
  * Template configuration
  */
 protected static function template(Page $page, $data = array())
 {
     // apply the basic template vars
     tpl::$data = array_merge(array('site' => static::$site, 'pages' => static::$site->children(), 'page' => $page), $data, static::controller($page, $data));
     return tpl::load($page->templateFile());
 }