Add page template with metadata display and copyright translation

Add support for language translations in footer

Add page template to template resolution logic
This commit is contained in:
Ruben 2025-10-02 20:16:52 +02:00
parent 19bb105303
commit d937ca6166
3 changed files with 122 additions and 64 deletions

View file

@ -23,7 +23,7 @@
<?= $content ?> <?= $content ?>
</main> </main>
<footer> <footer>
<p>&copy; <?= date('Y') ?></p> <p>&copy; <?= date('Y') ?> <?= htmlspecialchars($translations['footer_copyright'] ?? '') ?></p>
</footer> </footer>
</body> </body>
</html> </html>

View file

@ -0,0 +1,25 @@
<?= $content ?>
<?php if ($pageMetadata && (isset($pageMetadata['tags']) || isset($pageMetadata['categories']))): ?>
<aside class="metadata">
<?php if (!empty($pageMetadata['categories'])): ?>
<div class="categories">
<strong><?= htmlspecialchars($translations['categories'] ?? 'Categories') ?>:</strong>
<?php
$categories = array_map('trim', explode(',', $pageMetadata['categories']));
echo implode(', ', array_map('htmlspecialchars', $categories));
?>
</div>
<?php endif; ?>
<?php if (!empty($pageMetadata['tags'])): ?>
<div class="tags">
<strong><?= htmlspecialchars($translations['tags'] ?? 'Tags') ?>:</strong>
<?php
$tags = array_map('trim', explode(',', $pageMetadata['tags']));
echo implode(', ', array_map('htmlspecialchars', $tags));
?>
</div>
<?php endif; ?>
</aside>
<?php endif; ?>

View file

@ -28,12 +28,17 @@ $isDocsRequest = str_starts_with($requestPath, 'docs');
// Use docs templates for /docs, otherwise use custom/default templates // Use docs templates for /docs, otherwise use custom/default templates
if ($isDocsRequest) { if ($isDocsRequest) {
$baseTemplate = __DIR__ . '/default/docs/templates/base.php'; $baseTemplate = __DIR__ . '/default/docs/templates/base.php';
$pageTemplate = __DIR__ . '/default/docs/templates/base.php';
$listTemplate = __DIR__ . '/default/docs/templates/list.php'; $listTemplate = __DIR__ . '/default/docs/templates/list.php';
} else { } else {
$customBaseTemplate = dirname(__DIR__) . '/custom/templates/base.php'; $customBaseTemplate = dirname(__DIR__) . '/custom/templates/base.php';
$defaultBaseTemplate = __DIR__ . '/default/templates/base.php'; $defaultBaseTemplate = __DIR__ . '/default/templates/base.php';
$baseTemplate = file_exists($customBaseTemplate) ? $customBaseTemplate : $defaultBaseTemplate; $baseTemplate = file_exists($customBaseTemplate) ? $customBaseTemplate : $defaultBaseTemplate;
$customPageTemplate = dirname(__DIR__) . '/custom/templates/page.php';
$defaultPageTemplate = __DIR__ . '/default/templates/page.php';
$pageTemplate = file_exists($customPageTemplate) ? $customPageTemplate : $defaultPageTemplate;
$customListTemplate = dirname(__DIR__) . '/custom/templates/list.php'; $customListTemplate = dirname(__DIR__) . '/custom/templates/list.php';
$defaultListTemplate = __DIR__ . '/default/templates/list.php'; $defaultListTemplate = __DIR__ . '/default/templates/list.php';
$listTemplate = file_exists($customListTemplate) ? $customListTemplate : $defaultListTemplate; $listTemplate = file_exists($customListTemplate) ? $customListTemplate : $defaultListTemplate;
@ -195,6 +200,14 @@ function findCoverImage(string $dirPath): ?string {
return null; return null;
} }
function loadTranslations(string $lang): array {
$translationFile = dirname(__DIR__) . "/custom/languages/$lang.ini";
if (file_exists($translationFile)) {
return parse_ini_file($translationFile) ?: [];
}
return [];
}
function buildNavigation(string $contentDir, string $currentLang, string $defaultLang, array $pageFilePatterns): array { function buildNavigation(string $contentDir, string $currentLang, string $defaultLang, array $pageFilePatterns): array {
$navItems = []; $navItems = [];
@ -217,6 +230,8 @@ function buildNavigation(string $contentDir, string $currentLang, string $defaul
if ($currentLang !== $defaultLang) { if ($currentLang !== $defaultLang) {
$extensions = ['php', 'html', 'md']; $extensions = ['php', 'html', 'md'];
$hasContent = false; $hasContent = false;
// Check for language-specific content files
foreach ($extensions as $ext) { foreach ($extensions as $ext) {
if (file_exists("$itemPath/single.$currentLang.$ext") || if (file_exists("$itemPath/single.$currentLang.$ext") ||
file_exists("$itemPath/post.$currentLang.$ext") || file_exists("$itemPath/post.$currentLang.$ext") ||
@ -226,11 +241,17 @@ function buildNavigation(string $contentDir, string $currentLang, string $defaul
break; break;
} }
} }
// If no language-specific files, check if metadata has title for this language
if (!$hasContent && $metadata && isset($metadata['title'])) {
$hasContent = true;
}
if (!$hasContent) continue; if (!$hasContent) continue;
} }
// Extract title and build URL // Extract title and build URL
$title = $metadata['title'] ?? extractTitle($itemPath, $pageFilePatterns) ?? $item; $title = $metadata['title'] ?? extractTitle($itemPath, $pageFilePatterns) ?? ucfirst($item);
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : ''; $langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
// Use translated slug if available // Use translated slug if available
@ -261,17 +282,20 @@ function renderTemplate(string $content, int $statusCode = 200): void {
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang); $frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
$homeLabel = $frontpageMetadata['slug'] ?? 'Home'; $homeLabel = $frontpageMetadata['slug'] ?? 'Home';
// Load translations
$translations = loadTranslations($currentLang);
http_response_code($statusCode); http_response_code($statusCode);
include $baseTemplate; include $baseTemplate;
exit; exit;
} }
function renderFile(string $filePath): void { function renderFile(string $filePath): void {
global $baseTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns; global $baseTemplate, $pageTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns;
$realPath = realpath($filePath); $realPath = realpath($filePath);
if (!$realPath || !str_starts_with($realPath, $contentDir) || !is_readable($realPath)) { if (!$realPath || !str_starts_with($realPath, $contentDir) || !is_readable($realPath)) {
renderTemplate("<h1>403 Forbidden</h1><p>Access denied.</p>", 403); renderTemplate("<article><h1>403 Forbidden</h1><p>Access denied.</p></article>", 403);
} }
$ext = pathinfo($realPath, PATHINFO_EXTENSION); $ext = pathinfo($realPath, PATHINFO_EXTENSION);
@ -300,6 +324,15 @@ function renderFile(string $filePath): void {
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang); $frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
$homeLabel = $frontpageMetadata['slug'] ?? 'Home'; $homeLabel = $frontpageMetadata['slug'] ?? 'Home';
// Load translations
$translations = loadTranslations($currentLang);
// Wrap content with page template
ob_start();
include $pageTemplate;
$content = ob_get_clean();
// Wrap with base template
include $baseTemplate; include $baseTemplate;
exit; exit;
} }