Optimize content rendering and plugin loading order

Load metadata and plugins before content rendering to ensure
plugin-provided variables are available to PHP content files
This commit is contained in:
Ruben 2026-01-16 22:03:20 +01:00
parent d54cdc7ce1
commit d446ab1896

View file

@ -20,7 +20,7 @@ function renderContentFile(string $filePath, ?Context $ctx = null): string {
if (!class_exists('ParsedownExtra')) {
require_once __DIR__ . '/vendor/ParsedownExtra.php';
}
$html = '<article>' . (new ParsedownExtra())->text(file_get_contents($filePath)) . '</article>';
$html = (new ParsedownExtra())->text(file_get_contents($filePath));
if ($langPrefix) {
$html = preg_replace(
@ -73,13 +73,14 @@ function renderFile(Context $ctx, string $filePath): void {
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
if (in_array($ext, CONTENT_EXTENSIONS)) {
$content = renderContentFile($realPath, $ctx);
// 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;
@ -122,15 +123,16 @@ function renderFile(Context $ctx, string $filePath): void {
}
function renderMultipleFiles(Context $ctx, array $files, string $pageDir): void {
// Load metadata and page plugins BEFORE rendering content files
// so that plugin-provided template variables are available to PHP content files
$pageMetadata = loadMetadata($pageDir);
getPluginManager()->loadPagePlugins($pageMetadata);
$content = '';
foreach ($files as $file) {
$content .= renderContentFile($file, $ctx);
}
$pageMetadata = loadMetadata($pageDir);
getPluginManager()->loadPagePlugins($pageMetadata);
$navigation = $ctx->navigation;
$homeLabel = $ctx->homeLabel;
$pageTitle = $pageMetadata['title'] ?? null;