pancake-web-template 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pancake_web_template-0.1.0/LICENSE +21 -0
- pancake_web_template-0.1.0/PKG-INFO +25 -0
- pancake_web_template-0.1.0/README.md +3 -0
- pancake_web_template-0.1.0/pancake_web_template/__init__.py +40 -0
- pancake_web_template-0.1.0/pancake_web_template/template.py +94 -0
- pancake_web_template-0.1.0/pyproject.toml +21 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PancakeFramework
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pancake-web-template
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pancake 模板渲染插件 — Jinja2 模板引擎,为 pancake-web 提供 HTML 页面渲染
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: drayee
|
|
8
|
+
Author-email: 1473443474@qq.com
|
|
9
|
+
Requires-Python: >=3.10,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Dist: jinja2 (>=3.1.0,<4.0.0)
|
|
18
|
+
Requires-Dist: pancake-web (>=0.1.0)
|
|
19
|
+
Requires-Dist: pancake_framework (>=0.2.0)
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# Pancake-Web-Template
|
|
23
|
+
|
|
24
|
+
Pancake 模板渲染插件 — Jinja2 模板引擎,为 pancake-web 提供 HTML 页面渲染。
|
|
25
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Pancake 模板渲染插件 — Jinja2 模板引擎"""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from pancake.ovenware import InitAction
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Main(InitAction):
|
|
11
|
+
"""模板渲染插件入口
|
|
12
|
+
|
|
13
|
+
init_order=51, 在 web(50) 之后加载。
|
|
14
|
+
通过猴子补丁扩展 web 插件的响应解析,支持模板渲染。
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
init_order = 51
|
|
18
|
+
build_order = 0
|
|
19
|
+
|
|
20
|
+
def __init__(self):
|
|
21
|
+
from pancake.registry import export
|
|
22
|
+
|
|
23
|
+
from pancake_web_template.template import render, template, register_filter, _patch_resolve_response
|
|
24
|
+
export(render)
|
|
25
|
+
export(template)
|
|
26
|
+
export(register_filter)
|
|
27
|
+
|
|
28
|
+
# 猴子补丁: 扩展 web 插件的 resolve_response
|
|
29
|
+
_patch_resolve_response()
|
|
30
|
+
|
|
31
|
+
logger.info("模板渲染插件已加载")
|
|
32
|
+
|
|
33
|
+
def check(self) -> bool:
|
|
34
|
+
"""环境检查"""
|
|
35
|
+
try:
|
|
36
|
+
import jinja2
|
|
37
|
+
return True
|
|
38
|
+
except ImportError:
|
|
39
|
+
logger.error("缺少 jinja2 依赖,请执行: pip install jinja2")
|
|
40
|
+
return False
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Jinja2 模板引擎封装"""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from jinja2 import Environment, FileSystemLoader
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
_env: Environment = None
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _get_env() -> Environment:
|
|
14
|
+
"""获取或创建 Jinja2 Environment(单例)"""
|
|
15
|
+
global _env
|
|
16
|
+
if _env is None:
|
|
17
|
+
from pancake import settings
|
|
18
|
+
|
|
19
|
+
template_dir = settings.get("pancake.web.templates") or os.path.join("src", "templates")
|
|
20
|
+
debug = settings.get("pancake.web.debug") or False
|
|
21
|
+
|
|
22
|
+
if not os.path.isdir(template_dir):
|
|
23
|
+
os.makedirs(template_dir, exist_ok=True)
|
|
24
|
+
logger.info(f"模板目录已创建: {template_dir}")
|
|
25
|
+
|
|
26
|
+
loader = FileSystemLoader(template_dir)
|
|
27
|
+
_env = Environment(loader=loader, auto_reload=debug)
|
|
28
|
+
logger.info(f"Jinja2 引擎已初始化: {template_dir}")
|
|
29
|
+
return _env
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def render(template_name: str, **context):
|
|
33
|
+
"""渲染模板,返回 HtmlResponse
|
|
34
|
+
|
|
35
|
+
Usage:
|
|
36
|
+
return render("home.html", title="首页", items=[1, 2, 3])
|
|
37
|
+
"""
|
|
38
|
+
from pancake_web.response import HtmlResponse
|
|
39
|
+
|
|
40
|
+
env = _get_env()
|
|
41
|
+
tmpl = env.get_template(template_name)
|
|
42
|
+
html = tmpl.render(**context)
|
|
43
|
+
return HtmlResponse(html)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def template(template_name: str):
|
|
47
|
+
"""@template — 标记 handler 使用的模板名
|
|
48
|
+
|
|
49
|
+
handler 返回 dict 时自动渲染为 HTML 页面。
|
|
50
|
+
|
|
51
|
+
Usage:
|
|
52
|
+
@get("/home")
|
|
53
|
+
@template("home.html")
|
|
54
|
+
async def home(self, request):
|
|
55
|
+
return {"title": "首页", "items": [1, 2, 3]}
|
|
56
|
+
"""
|
|
57
|
+
def decorator(func):
|
|
58
|
+
func._template_name = template_name
|
|
59
|
+
return func
|
|
60
|
+
return decorator
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def register_filter(name: str, func):
|
|
64
|
+
"""注册 Jinja2 自定义过滤器
|
|
65
|
+
|
|
66
|
+
Usage:
|
|
67
|
+
register_filter("reverse", lambda s: s[::-1])
|
|
68
|
+
"""
|
|
69
|
+
env = _get_env()
|
|
70
|
+
env.filters[name] = func
|
|
71
|
+
logger.debug(f"注册模板过滤器: {name}")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _patch_resolve_response():
|
|
75
|
+
"""猴子补丁: 扩展 web 插件的 resolve_response,支持 @template 自动渲染"""
|
|
76
|
+
import pancake_web.decorators as web_decorators
|
|
77
|
+
|
|
78
|
+
_original_resolve = web_decorators.resolve_response
|
|
79
|
+
|
|
80
|
+
async def _patched_resolve(result, _handler=None):
|
|
81
|
+
# 如果 handler 有 @template 标记且返回 dict,渲染模板
|
|
82
|
+
if _handler and isinstance(result, dict) and hasattr(_handler, "_template_name"):
|
|
83
|
+
logger.info(f"渲染模板: {_handler._template_name}")
|
|
84
|
+
return render(_handler._template_name, **result)
|
|
85
|
+
return await _original_resolve(result)
|
|
86
|
+
|
|
87
|
+
web_decorators.resolve_response = _patched_resolve
|
|
88
|
+
logger.debug("resolve_response 已补丁")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def reset_env():
|
|
92
|
+
"""重置 Jinja2 环境(用于测试)"""
|
|
93
|
+
global _env
|
|
94
|
+
_env = None
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "pancake-web-template"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Pancake 模板渲染插件 — Jinja2 模板引擎,为 pancake-web 提供 HTML 页面渲染"
|
|
5
|
+
authors = ["drayee <1473443474@qq.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
packages = [{include = "pancake_web_template"}]
|
|
9
|
+
|
|
10
|
+
[tool.poetry.dependencies]
|
|
11
|
+
python = "^3.10"
|
|
12
|
+
pancake_framework = ">=0.2.0"
|
|
13
|
+
pancake-web = ">=0.1.0"
|
|
14
|
+
jinja2 = "^3.1.0"
|
|
15
|
+
|
|
16
|
+
[tool.poetry.plugins."pancake.plugins"]
|
|
17
|
+
web_template = "pancake_web_template:Main"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["poetry-core"]
|
|
21
|
+
build-backend = "poetry.core.masonry.api"
|