Add 'page' Twig function to return a single page

This commit is contained in:
Daniel Rudolf 2023-01-05 21:50:42 +01:00
parent a5fefa46fb
commit 35d894ec1a
No known key found for this signature in database
GPG key ID: A061F02CD8DE4538
2 changed files with 15 additions and 0 deletions

View file

@ -31,6 +31,7 @@ Released: -
* [New] `Pico::prepareFileContent()` and `Pico::substituteFileContent()` both * [New] `Pico::prepareFileContent()` and `Pico::substituteFileContent()` both
now receive the (optional) `$pageId` argument for the new `%page_*%` now receive the (optional) `$pageId` argument for the new `%page_*%`
Markdown placeholders 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 * [Changed] ! Pico now requires PHP 7.2.5 or later (this includes full PHP 8
support, also see #528, #534, #608) support, also see #528, #534, #608)
* [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this * [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this

View file

@ -99,6 +99,7 @@ class PicoTwigExtension extends AbstractTwigExtension
return [ return [
'url_param' => new TwigFunction('url_param', [ $this, 'urlParamFunction' ]), 'url_param' => new TwigFunction('url_param', [ $this, 'urlParamFunction' ]),
'form_param' => new TwigFunction('form_param', [ $this, 'formParamFunction' ]), 'form_param' => new TwigFunction('form_param', [ $this, 'formParamFunction' ]),
'page' => new TwigFunction('page', [ $this, 'pageFunction' ]),
'pages' => new TwigFunction('pages', [ $this, 'pagesFunction' ]), 'pages' => new TwigFunction('pages', [ $this, 'pagesFunction' ]),
]; ];
} }
@ -322,6 +323,19 @@ class PicoTwigExtension extends AbstractTwigExtension
return $this->pico->getFormParameter($name, $filter, $options, $flags); 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 * Returns all pages within a particular branch of Pico's page tree
* *