Browse Source

feat: liquid模板例子

igb 5 months ago
parent
commit
83bbe5caf8

+ 26 - 0
app/Http/Controllers/DemoController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Controllers;
+use Illuminate\Http\Request;
+use App\Services\LiquidRenderer;
+class DemoController extends Controller
+{
+
+    /**
+     * Liquid 模板例子
+     * @return
+     * @throws
+     */
+    public function index()
+    {
+
+            //模板支持多级目录,可以用 . 代替目录分隔符
+            $output = LiquidRenderer::render('demo.template.liquid', [
+                'name' => 'John Doe',
+                'message' => 'Hello, Liquid!'
+            ]);
+
+        return response($output);
+
+    }
+}

+ 61 - 0
app/Services/LiquidRenderer.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Services;
+
+use Liquid\Template;
+use Liquid\Cache\File as FileCache;
+use Illuminate\Support\Facades\File;
+
+class LiquidRenderer
+{
+    protected static $baseTemplatePath;
+
+    public static function render(string $templateName, array $data = []): string
+    {
+        // 初始化基础模板路径(仅在首次调用时)
+        if (!self::$baseTemplatePath) {
+            self::$baseTemplatePath = config('liquid.template_path');
+        }
+
+
+        // 查找模板文件
+        $templatePath = self::findTemplate($templateName);
+
+        if (!$templatePath) {
+            throw new \Exception("Template not found: {$templateName}");
+        }
+
+        // 读取 Liquid 模板文件内容
+        $templateContent = File::get($templatePath);
+
+        // 创建并解析模板
+        $template = new Template();
+        $template->setCache(new FileCache(['cache_dir' => storage_path('framework/cache/data')]));
+        $template->parse($templateContent);
+
+        // 渲染模板并返回结果
+        return $template->render($data);
+    }
+
+    protected static function findTemplate(string $templateName): ?string
+    {
+        // 如果模板名没有以 .liquid 结尾,则添加 .liquid 扩展名
+        if (pathinfo($templateName, PATHINFO_EXTENSION) !== 'liquid') {
+            $templateName .= '.liquid';
+        }
+
+        // 处理模板名中的 . 号,将其转换为目录分隔符(不包括扩展名)
+        $templateNameWithoutExtension = pathinfo($templateName, PATHINFO_FILENAME);
+        $templatePathSegments = explode('.', $templateNameWithoutExtension);
+        $relativePath = implode(DIRECTORY_SEPARATOR, $templatePathSegments) . '.liquid';
+
+        // 构建预期的模板路径
+        $expectedPath = self::$baseTemplatePath . DIRECTORY_SEPARATOR . $relativePath;
+
+        if (File::exists($expectedPath)) {
+            return $expectedPath;
+        }
+
+        return null;
+    }
+}

+ 5 - 0
config/liquid.php

@@ -0,0 +1,5 @@
+<?php
+
+return [
+    'template_path' => resource_path('views/liquid'),
+];

+ 9 - 0
resources/views/liquid/demo/template.liquid

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>{{ name }}</title>
+</head>
+<body>
+<h1>welcome : {{name}} {{ message }}</h1>
+</body>
+</html>

+ 3 - 0
routes/web.php

@@ -2,6 +2,7 @@
 
 use Illuminate\Support\Facades\Route;
 
+use App\Http\Controllers\DemoController;
 /*
 |--------------------------------------------------------------------------
 | Web Routes
@@ -16,3 +17,5 @@ use Illuminate\Support\Facades\Route;
 Route::get('/', function () {
     return view('welcome');
 });
+
+Route::get('/demo', [DemoController::class, 'index']);