diff --git a/CHANGELOG.md b/CHANGELOG.md index 8162bf7..43ade98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Released: - * [New] `Pico::prepareFileContent()` and `Pico::substituteFileContent()` both now receive the (optional) `$pageId` argument for the new `%page_*%` Markdown placeholders +* [New] Add `page()` Twig function to access a page's data * [Changed] ! Pico now requires PHP 7.2.5 or later (this includes full PHP 8 support, also see #528, #534, #608) * [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this diff --git a/lib/PicoTwigExtension.php b/lib/PicoTwigExtension.php index 428dc03..61da550 100644 --- a/lib/PicoTwigExtension.php +++ b/lib/PicoTwigExtension.php @@ -99,6 +99,7 @@ class PicoTwigExtension extends AbstractTwigExtension return [ 'url_param' => new TwigFunction('url_param', [ $this, 'urlParamFunction' ]), 'form_param' => new TwigFunction('form_param', [ $this, 'formParamFunction' ]), + 'page' => new TwigFunction('page', [ $this, 'pageFunction' ]), 'pages' => new TwigFunction('pages', [ $this, 'pagesFunction' ]), ]; } @@ -322,6 +323,19 @@ class PicoTwigExtension extends AbstractTwigExtension return $this->pico->getFormParameter($name, $filter, $options, $flags); } + /** + * Returns the data of a single page + * + * @param string $id identifier of the page to return + * + * @return array|null the data of the page, or NULL + */ + public function pageFunction(string $id): ?array + { + $pages = $this->getPico()->getPages(); + return $pages[$id] ?? null; + } + /** * Returns all pages within a particular branch of Pico's page tree *