소스 검색

Add 'page' Twig function to return a single page

Daniel Rudolf 2 년 전
부모
커밋
35d894ec1a
2개의 변경된 파일15개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      CHANGELOG.md
  2. 14 0
      lib/PicoTwigExtension.php

+ 1 - 0
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

+ 14 - 0
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
      *