/** * use object buffering to build up the views into a single string and either return or echo it * optionally output any headers that have been added to the view instance * @param bool $echo whether to echo the view or return it as a string * @param bool $with_headers if set to true and $echo is also set to true, then send headers to the browser, otherwise do nothing * @return string */ public static function render($echo = true, $with_headers = false) { $v = view::getInstance(); $app = \maverick\maverick::getInstance(); $view_file_path = MAVERICK_VIEWSDIR . "/{$v->view}.php"; if (!file_exists($view_file_path)) { error::show("View '{$v->view}' does not exist"); } else { ob_start(); include $view_file_path; $view = ob_get_contents(); ob_end_clean(); if ($app->get_config('config.view_parsing') !== false) { $view = $v->parse_view($view); } // this just stores the view if the config value is set to cache it if ($app->get_config('cache.on') !== false) { $hash = $app->get_request_route_hash(); \maverick\cache::store($hash, $view); if ($with_headers) { \maverick\cache::store("{$hash}_headers", json_encode($v->headers)); } } if ($with_headers) { foreach ($v->headers as $header) { header($header); } // teapot if (isset($v->original_headers['status']) && $v->original_headers['status'] == 418 && $app->get_config('config.teapot') !== false) { $teapot = \helpers\html\html::load_snippet(\MAVERICK_BASEDIR . 'vendor/maverick/teapot.html', array()); $view = preg_replace('/<body/', "<body>{$teapot}", $view, 1); } } if ($echo) { echo $view; } else { return $view; } } }