Add page CSS URL and hash to template variables

Move page CSS handling from rendering to router
This commit is contained in:
Ruben 2026-02-05 23:30:20 +01:00
parent 7782eefa96
commit 696b0ad801
2 changed files with 4 additions and 61 deletions

View file

@ -64,63 +64,6 @@ function renderTemplate(Context $ctx, string $content, int $statusCode = 200): v
exit; exit;
} }
function renderFile(Context $ctx, string $filePath): void {
$realPath = realpath($filePath);
if (!$realPath || !str_starts_with($realPath, $ctx->contentDir) || !is_readable($realPath)) {
renderTemplate($ctx, "<article><h1>403 Forbidden</h1><p>Access denied.</p></article>", 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, "<article><h1>404 - Not Found</h1><p>The requested file could not be found.</p></article>", 404);
}
function renderMultipleFiles(Context $ctx, array $files, string $pageDir): void { function renderMultipleFiles(Context $ctx, array $files, string $pageDir): void {
// Load metadata and page plugins BEFORE rendering content files // Load metadata and page plugins BEFORE rendering content files

View file

@ -110,9 +110,6 @@ switch ($parsedPath['type']) {
$listTemplate = $ctx->templates->list; $listTemplate = $ctx->templates->list;
if (isset($metadata['page_template']) && !empty($metadata['page_template'])) { if (isset($metadata['page_template']) && !empty($metadata['page_template'])) {
$templateName = $metadata['page_template']; $templateName = $metadata['page_template'];
if (!str_ends_with($templateName, '.php')) {
$templateName .= '';
}
$customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php"; $customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php";
$defaultTemplate = __DIR__ . "/default/templates/$templateName.php"; $defaultTemplate = __DIR__ . "/default/templates/$templateName.php";
@ -186,6 +183,8 @@ switch ($parsedPath['type']) {
// Check for page-specific CSS // Check for page-specific CSS
$pageCss = findPageCss($dir, $ctx->contentDir); $pageCss = findPageCss($dir, $ctx->contentDir);
$pageCssUrl = $pageCss['url'] ?? null;
$pageCssHash = $pageCss['hash'] ?? null;
// Let plugins add template variables // Let plugins add template variables
$templateVars = Hooks::apply(Hook::TEMPLATE_VARS, [ $templateVars = Hooks::apply(Hook::TEMPLATE_VARS, [
@ -193,7 +192,8 @@ switch ($parsedPath['type']) {
'homeLabel' => $homeLabel, 'homeLabel' => $homeLabel,
'pageTitle' => $pageTitle, 'pageTitle' => $pageTitle,
'metaDescription' => $metaDescription, 'metaDescription' => $metaDescription,
'pageCss' => $pageCss, 'pageCssUrl' => $pageCssUrl,
'pageCssHash' => $pageCssHash,
'items' => $items, 'items' => $items,
'pageContent' => $pageContent 'pageContent' => $pageContent
], $ctx); ], $ctx);