|
@@ -1,32 +1,134 @@
|
|
|
<?php
|
|
|
namespace App\Services\LiquidTags;
|
|
|
|
|
|
-class LiquidTagPage
|
|
|
+use App\Models\SitePage; // 假设 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)
|
|
|
{
|
|
|
- // 正则表达式解析传入的文章 ID,例如 `{% page 1 %}`\
|
|
|
- $this->articleId= $markup;
|
|
|
-// $syntax = '/(\d+)/';
|
|
|
-// if (preg_match($syntax, $markup, $matches)) {
|
|
|
-// $this->articleId = (int)$matches[1];
|
|
|
-//
|
|
|
-// }
|
|
|
+
|
|
|
+ // 初始化默认值
|
|
|
+ $this->mode = 'single'; // 默认模式为单篇文章
|
|
|
+ $this->pageId = null;
|
|
|
+ $this->pageTitle = null;
|
|
|
+ $this->pageSlug = null;
|
|
|
+ $this->limit = 10; // 默认显示 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; // 获取 mode 参数
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// render 方法执行逻辑并返回渲染结果
|
|
|
- public function render(&$context)
|
|
|
+ public function render($context)
|
|
|
{
|
|
|
- if ($this->articleId) {
|
|
|
+ // 根据 mode 渲染不同的内容
|
|
|
+ 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>';
|
|
|
+ // 使用 LiquidRenderer 渲染模板
|
|
|
+ return LiquidRenderer::render($this->templateFile, $data);
|
|
|
}
|
|
|
}
|