调整版本并做测试
This commit is contained in:
89
render_example.py
Normal file
89
render_example.py
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3.9
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Example demonstrating how to use the render function
|
||||
"""
|
||||
|
||||
from pyenjoy.template.Engine import Engine
|
||||
|
||||
# Initialize the engine (global instance)
|
||||
engine = Engine.use()
|
||||
|
||||
# Configure the engine (optional)
|
||||
engine.base_template_path = "./templates" # Set base template directory
|
||||
engine.encoding = "utf-8" # Set template encoding
|
||||
engine.dev_mode = True # Enable auto-reload for development
|
||||
|
||||
def render(page, data):
|
||||
"""
|
||||
Render a template file with provided data
|
||||
|
||||
Args:
|
||||
page: Template file name (relative to base_template_path)
|
||||
data: Dictionary with template data
|
||||
|
||||
Returns:
|
||||
Rendered template string
|
||||
"""
|
||||
template = engine.get_template(page)
|
||||
result = template.render_to_string(data)
|
||||
return result
|
||||
|
||||
def render_string(template_content, data):
|
||||
"""
|
||||
Render a template string with provided data
|
||||
|
||||
Args:
|
||||
template_content: Template content as string
|
||||
data: Dictionary with template data
|
||||
|
||||
Returns:
|
||||
Rendered template string
|
||||
"""
|
||||
template = engine.get_template_by_string(template_content)
|
||||
result = template.render_to_string(data)
|
||||
return result
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
# Example 1: Render from file
|
||||
print("Example 1: Render from file")
|
||||
print("=" * 50)
|
||||
try:
|
||||
# Create a simple template file for testing
|
||||
import os
|
||||
# os.makedirs("./templates", exist_ok=True)
|
||||
# with open("./templates/test.html", "w", encoding="utf-8") as f:
|
||||
# f.write("Hello, #(name)!")
|
||||
|
||||
# Render the template
|
||||
data = {"name": "World", "items": [
|
||||
{"id": 1, "title": "文章1", "author": "张三", "tags": ["cdms", "管理系统"], "create_time": "2023-01-01"},
|
||||
{"id": 2, "title": "文章2", "author": "李四", "tags": ["python"], "create_time": "2023-02-01"}
|
||||
]}
|
||||
result = render("test.html", data)
|
||||
print(f"Rendered result: {result}")
|
||||
|
||||
# Clean up
|
||||
# os.remove("./templates/test.html")
|
||||
# os.rmdir("./templates")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
# Example 2: Render from string
|
||||
print("\nExample 2: Render from string")
|
||||
print("=" * 50)
|
||||
template_content = "Name: #(name), Age: #(age??'未知')"
|
||||
data = {"name": "张三"}
|
||||
result = render_string(template_content, data)
|
||||
print(f"Rendered result: {result}")
|
||||
|
||||
# Example 3: Render with filters
|
||||
print("\nExample 3: Render with filters")
|
||||
print("=" * 50)
|
||||
template_content = "Tags: #(tags | join(', '))"
|
||||
data = {"tags": ["cdms", "管理系统"]}
|
||||
result = render_string(template_content, data)
|
||||
print(f"Rendered result: {result}")
|
||||
|
||||
print("\nAll examples completed!")
|
||||
Reference in New Issue
Block a user