/** * @param \DOMDocument $xml * @param bool|string $specificInstance * @param bool $dontEcho * @param bool $dontFillXML * @param bool $normalize * @return bool|string * @throws Exception */ public static function render(&$xml, $specificInstance = false, $dontEcho = false, $dontFillXML = false, $normalize = true) { if ($specificInstance) { $instance = $specificInstance; } elseif (self::$instance) { $instance = self::$instance; } else { $instance = 'main'; } Debugger::addLine("Render start (instance '{$instance}')"); if (!($resource = Resourcer::getInstance('xslt')->compile($instance))) { throw new Exception("XSLT resource not found"); } $xslDom = new \DomDocument(); $xslDom->resolveExternals = true; $xslDom->substituteEntities = true; if (!$xslDom->loadXML($resource)) { throw new Exception("XSLT load problem for instance '{$instance}'"); } $xslProcessor = new \XSLTProcessor(); $xslProcessor->importStylesheet($xslDom); if (!$dontFillXML and !HttpError::$error and !Debugger::$shutdown) { View\XML::fillXML($xml, $instance); } // transform template if ($html = $xslProcessor->transformToDoc($xml)) { if ($normalize) { $html = self::normalize($html); } else { $html->formatOutput = true; $html = $html->saveXML(); } if ($dontEcho) { return $html; } echo $html; self::$rendered = true; if (Debugger::isEnabled()) { echo '<!-- Page rendered in ' . Debugger::getTimer() . ' seconds -->'; } if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } } else { $errormsg = libxml_get_errors(); //error_get_last(); throw new Exception($errormsg ? $errormsg['message'] : "Can't render templates"); } return true; }
/** * Choose view depending on request type */ public static final function start() { $controller = Controller::getInstance(); if (Controller::hasUnusedParameters()) { $controller->putExpires(true); throw new HttpError(404); } elseif (!is_null(self::$output)) { $controller->putExpires(); header('Content-Type: ' . self::$outputType . '; charset="utf-8"'); echo self::$output; View::$rendered = true; } elseif (Debugger::isEnabled() and isset($_GET['xml']) and $_GET['xml']) { if ($_GET['xml'] == '2') { View\XML::fillXML(); } header('Content-Type: text/xml; charset="utf-8"'); $controller->xml->formatOutput = true; $controller->xml->encoding = 'utf-8'; echo rawurldecode($controller->xml->saveXML()); View::$rendered = true; } elseif (!View::$rendered and Request::isAjax()) { $controller->putExpires(); // should be application/json, but opera doesn't understand it and offers to save file to disk header('Content-type: text/plain'); echo Ajaxer::getResponse(); View::$rendered = true; } elseif (!View::$rendered) { $controller->putExpires(); try { View::render($controller->xml); } catch (HttpError $ex) { if (!Debugger::isConsoleEnabled()) { throw new HttpError(500); } else { echo Debugger::debugHTML(true); die; } } } }