|
@@ -1,32 +1,134 @@
|
|
|
<?php
|
|
|
namespace App\Services\LiquidTags;
|
|
|
|
|
|
-class LiquidTagPage
|
|
|
+use App\Models\SitePage;
|
|
|
+use Liquid\AbstractBlock;
|
|
|
+use App\Services\LiquidRenderer;
|
|
|
+
|
|
|
+class LiquidTagPage extends AbstractBlock
|
|
|
{
|
|
|
- private $articleId;
|
|
|
+ private $mode;
|
|
|
+ private $pageId;
|
|
|
+ private $pageTitle;
|
|
|
+ private $pageSlug;
|
|
|
+ private $limit;
|
|
|
+ private $templateFile;
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
public function __construct($markup, array &$tokens, $file_system = null)
|
|
|
{
|
|
|
-
|
|
|
- $this->articleId= $markup;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ $this->mode = 'single';
|
|
|
+ $this->pageId = null;
|
|
|
+ $this->pageTitle = null;
|
|
|
+ $this->pageSlug = null;
|
|
|
+ $this->limit = 10;
|
|
|
+ $this->templateFile = null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $syntax = '/(\w+)=("[^"]*"|\'[^\']*\'|\d+)/';
|
|
|
+
|
|
|
+ if (preg_match_all($syntax, $markup, $matches, PREG_SET_ORDER)) {
|
|
|
+ foreach ($matches as $match) {
|
|
|
+ $key = $match[1];
|
|
|
+ $value = trim($match[2], '\"\'');
|
|
|
+
|
|
|
+ switch ($key) {
|
|
|
+ case 'mode':
|
|
|
+ $this->mode = $value;
|
|
|
+ break;
|
|
|
+ case 'id':
|
|
|
+ $this->pageId = (int)$value;
|
|
|
+ break;
|
|
|
+ case 'title':
|
|
|
+ $this->pageTitle = $value;
|
|
|
+ break;
|
|
|
+ case 'slug':
|
|
|
+ $this->pageSlug = $value;
|
|
|
+ break;
|
|
|
+ case 'limit':
|
|
|
+ $this->limit = (int)$value;
|
|
|
+ break;
|
|
|
+ case 'template':
|
|
|
+ $this->templateFile = $value;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
- public function render(&$context)
|
|
|
+ public function render($context)
|
|
|
{
|
|
|
- if ($this->articleId) {
|
|
|
+
|
|
|
+ if ($this->mode === 'list') {
|
|
|
+ return $this->renderList();
|
|
|
+ }
|
|
|
|
|
|
- return $this->articleId;
|
|
|
+
|
|
|
+ return $this->renderSingle();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private function renderList()
|
|
|
+ {
|
|
|
+
|
|
|
+ $pages = SitePage::getPages($this->limit);
|
|
|
+
|
|
|
+
|
|
|
+ if ($pages->count() > 0) {
|
|
|
+ return $this->renderTemplate(['pages' => $pages->toArray()]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ private function renderSingle()
|
|
|
+ {
|
|
|
+
|
|
|
+ if ($this->pageId) {
|
|
|
+ return $this->renderPage(SitePage::getPageById($this->pageId));
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->pageTitle) {
|
|
|
+ return $this->renderPage(SitePage::getPageByTitle($this->pageTitle));
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->pageSlug) {
|
|
|
+ return $this->renderPage(SitePage::getPageBySlug($this->pageSlug));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private function renderPage($page)
|
|
|
+ {
|
|
|
+
|
|
|
+ if ($page) {
|
|
|
+ return $this->renderTemplate(['page' => $page->toArray()]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private function renderTemplate(array $data)
|
|
|
+ {
|
|
|
+ if (!$this->templateFile) {
|
|
|
+ return '';
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- return '<p>文章不存在。</p>';
|
|
|
+
|
|
|
+ return LiquidRenderer::render($this->templateFile, $data);
|
|
|
}
|
|
|
}
|