From 696b0ad801c9f8eeea5cb6302bf86fca8fa08416 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 5 Feb 2026 23:30:20 +0100 Subject: [PATCH] Claenup Add page CSS URL and hash to template variables Move page CSS handling from rendering to router --- app/rendering.php | 57 ----------------------------------------------- app/router.php | 8 +++---- 2 files changed, 4 insertions(+), 61 deletions(-) diff --git a/app/rendering.php b/app/rendering.php index 7ed8670..7d61f89 100644 --- a/app/rendering.php +++ b/app/rendering.php @@ -64,63 +64,6 @@ function renderTemplate(Context $ctx, string $content, int $statusCode = 200): v exit; } -function renderFile(Context $ctx, string $filePath): void { - $realPath = realpath($filePath); - if (!$realPath || !str_starts_with($realPath, $ctx->contentDir) || !is_readable($realPath)) { - renderTemplate($ctx, "

403 Forbidden

Access denied.

", 403); - } - - $ext = pathinfo($realPath, PATHINFO_EXTENSION); - - if (in_array($ext, CONTENT_EXTENSIONS)) { - // Load metadata and page plugins BEFORE rendering content - // so that plugin-provided template variables are available to PHP content files - $pageDir = dirname($realPath); - $pageMetadata = loadMetadata($pageDir); - getPluginManager()->loadPagePlugins($pageMetadata); - - $content = renderContentFile($realPath, $ctx); - - $navigation = $ctx->navigation; - $homeLabel = $ctx->homeLabel; - $pageTitle = $pageMetadata['title'] ?? null; - $metaDescription = extractMetaDescription($pageDir, $pageMetadata); - - $pageCss = findPageCss($pageDir, $ctx->contentDir); - $pageCssUrl = $pageCss['url'] ?? null; - $pageCssHash = $pageCss['hash'] ?? null; - - $coverImage = findCoverImage($pageDir); - $socialImageUrl = null; - if ($coverImage) { - $relativePath = str_replace($ctx->contentDir, '', $pageDir); - $relativePath = trim($relativePath, '/'); - $socialImageUrl = '/' . ($relativePath ? $relativePath . '/' : '') . $coverImage; - } - - $templateVars = Hooks::apply(Hook::TEMPLATE_VARS, [ - 'content' => $content, - 'navigation' => $navigation, - 'homeLabel' => $homeLabel, - 'pageTitle' => $pageTitle, - 'metaDescription' => $metaDescription, - 'pageCssUrl' => $pageCssUrl, - 'pageCssHash' => $pageCssHash, - 'socialImageUrl' => $socialImageUrl - ], $ctx); - - extract($templateVars); - - ob_start(); - require $ctx->templates->page; - $wrappedContent = ob_get_clean(); - - include $ctx->templates->base; - exit; - } - - renderTemplate($ctx, "

404 - Not Found

The requested file could not be found.

", 404); -} function renderMultipleFiles(Context $ctx, array $files, string $pageDir): void { // Load metadata and page plugins BEFORE rendering content files diff --git a/app/router.php b/app/router.php index c4e61dd..64e706a 100644 --- a/app/router.php +++ b/app/router.php @@ -110,9 +110,6 @@ switch ($parsedPath['type']) { $listTemplate = $ctx->templates->list; if (isset($metadata['page_template']) && !empty($metadata['page_template'])) { $templateName = $metadata['page_template']; - if (!str_ends_with($templateName, '.php')) { - $templateName .= ''; - } $customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php"; $defaultTemplate = __DIR__ . "/default/templates/$templateName.php"; @@ -186,6 +183,8 @@ switch ($parsedPath['type']) { // Check for page-specific CSS $pageCss = findPageCss($dir, $ctx->contentDir); + $pageCssUrl = $pageCss['url'] ?? null; + $pageCssHash = $pageCss['hash'] ?? null; // Let plugins add template variables $templateVars = Hooks::apply(Hook::TEMPLATE_VARS, [ @@ -193,7 +192,8 @@ switch ($parsedPath['type']) { 'homeLabel' => $homeLabel, 'pageTitle' => $pageTitle, 'metaDescription' => $metaDescription, - 'pageCss' => $pageCss, + 'pageCssUrl' => $pageCssUrl, + 'pageCssHash' => $pageCssHash, 'items' => $items, 'pageContent' => $pageContent ], $ctx);