Implement per-page template functionality (#45)

Can be enabled by adding "Template: templatename" to a file header.
Then AntCMS will attempt to load the associated template from the  "Templates" directory of your current theme
This commit is contained in:
Belle Aerni 2023-05-27 00:24:28 -07:00 committed by GitHub
parent 38e51166c3
commit 945881365e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -31,7 +31,7 @@ class AntCMS
$this->renderException("404");
}
$pageTemplate = $this->getPageLayout(null, $page);
$pageTemplate = $this->getPageLayout(null, $page, $content['template']);
$params = [
'AntCMSTitle' => $content['title'],
@ -61,9 +61,10 @@ class AntCMS
* @param string $currentPage optional - What page is the active page.
* @return string the default page layout
*/
public static function getPageLayout(string $theme = null, string $currentPage = '')
public static function getPageLayout(string $theme = null, string $currentPage = '', string | null $template = null)
{
$pageTemplate = self::getThemeTemplate('default', $theme);
$layout = empty($template) ? 'default' : $template;
$pageTemplate = self::getThemeTemplate($layout, $theme);
return str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation(self::getThemeTemplate('nav', $theme), $currentPage), $pageTemplate);
}
@ -110,7 +111,7 @@ class AntCMS
$pageHeaders = AntCMS::getPageHeaders($pageContent);
// Remove the AntCMS section from the content
$pageContent = preg_replace('/\A--AntCMS--.*?--AntCMS--/sm', '', $pageContent);
return ['content' => $pageContent, 'title' => $pageHeaders['title'], 'author' => $pageHeaders['author'], 'description' => $pageHeaders['description'], 'keywords' => $pageHeaders['keywords']];
return ['content' => $pageContent, 'title' => $pageHeaders['title'], 'author' => $pageHeaders['author'], 'description' => $pageHeaders['description'], 'keywords' => $pageHeaders['keywords'], 'template' => $pageHeaders['template']];
} catch (\Exception) {
return false;
}
@ -191,8 +192,6 @@ class AntCMS
if (isset($matches[0])) {
$header = $matches[0];
// Then remove it from the page content so it doesn't cause issues if we try to generate the keywords
$pageContent = str_replace($header, '', $pageContent);
preg_match('/Title: (.*)/', $header, $matches);
$pageHeaders['title'] = trim($matches[1] ?? 'AntCMS');
@ -205,6 +204,9 @@ class AntCMS
preg_match('/Keywords: (.*)/', $header, $matches);
$pageHeaders['keywords'] = trim($matches[1] ?? '');
preg_match('/Template: (.*)/', $header, $matches);
$pageHeaders['template'] = trim($matches[1] ?? '');
}
return $pageHeaders;