title = "Untitled Document"; $this->templateName = $templateName; $this->parameters = $params; $this->twigLoader = new FilesystemLoader(self::getTemplatePaths()); $this->twigEnvironment = new Environment($this->twigLoader, [ 'cache' => WEBROOT . '/Site/Cache/Templates/', 'auto_reload' => true ]); $this->twigEnvironment->addExtension(new CustomTwigFunctions()); } public function getTitle(): string { return $this->title; } protected function getTemplateName(): string { return $this->templateName; } protected function loadParameters() { } protected function setTemplate(string $file) { $this->templateName = $file; } public function getCode(array $params = []): string { $this->loadParameters(); return $this->renderTemplate($this->templateName, $this->parameters); } public function renderTemplate(string $name, array $params = []): string { try { $context = $this->getContext(); $session = $context->getSession(); $settings = $this->getSettings(); $language = $context->getLanguage(); $urlParts = parse_url($this->getRouter()->getRequestedUri()); $params = array_replace_recursive([ "user" => [ "lang" => $language->getShortCode(), "loggedIn" => $session !== null, "session" => ($session ? [ "csrfToken" => $session->getCsrfToken() ] : null) ], "site" => [ "name" => $settings->getSiteName(), "url" => [ "base" => $settings->getBaseUrl(), "path" => $urlParts["path"], "query" => $urlParts["query"] ?? "", "fragment" => $urlParts["fragment"] ?? "" ], "lastModified" => date(L('Y-m-d H:i:s'), @filemtime(self::getTemplatePath($name))), "registrationEnabled" => $settings->isRegistrationAllowed(), "title" => $this->title, "recaptcha" => [ "key" => $settings->isRecaptchaEnabled() ? $settings->getRecaptchaSiteKey() : null, "enabled" => $settings->isRecaptchaEnabled(), ], "csp" => [ "nonce" => $this->getCSPNonce(), "enabled" => $this->isCSPEnabled() ], "language" => [ "code" => $language->getCode(), "shortCode" => $language->getShortCode(), "name" => $language->getName(), "entries" => $language->getEntries() ] ] ], $params); return $this->twigEnvironment->render($name, $params); } catch (LoaderError | RuntimeError | SyntaxError $e) { return "Error rendering twig template: " . htmlspecialchars($e->getMessage()) . ""; } } protected function loadView(string $class): array { $view = new $class($this); $view->loadParameters($this->parameters); if ($view->getTitle()) { $this->title = $view->getTitle(); } return $this->parameters; } public function doSearch(SearchQuery $query, DocumentRoute $route): array { $this->loadParameters(); $viewParams = $this->parameters["view"] ?? []; $siteTitle = $this->parameters["site"]["title"] ?? $this->title; $results = Searchable::searchArray($viewParams, $query); return array_map(function ($res) use ($siteTitle, $route) { return new SearchResult($route->getUrl(), $siteTitle, $res["text"]); }, $results); } }