Files
py-enjoy/README.md
2026-02-28 12:07:17 +08:00

62 lines
1.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# py-enjoy
jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现。
python port by mrzhou@miw.cn
### 使用样例
#### 示例1使用项目内置的jar文件推荐
```python
from enjoy import Enjoy
if __name__ == "__main__":
# 1. 配置参数(仅需配置模板路径)
TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下
# 2. 创建Enjoy实例初始化Engine自动使用内置的enjoy-5.2.2.jar
try:
enjoy = Enjoy(
template_base_path=TEMPLATE_BASE_PATH
)
```
#### 示例2指定自定义的jar文件路径
```python
from enjoy import Enjoy
if __name__ == "__main__":
# 1. 配置参数需配置jar路径和模板路径
JAR_PATH = "enjoy-5.2.2.jar" # enjoy-5.2.2.jar的绝对/相对路径
TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下
# 2. 创建Enjoy实例初始化Engine
try:
enjoy = Enjoy(
jar_path=JAR_PATH,
template_base_path=TEMPLATE_BASE_PATH
)
# 3. 准备模板参数
render_data = {
"name": "JFinal Enjoy 5.2.2测试",
"hobbies": ["Python调用Java", "模板渲染", "跨语言开发"],
"user": {"age": 25, "gender": ""}
}
# 4. 渲染模板tempStr是模板文件名相对于TEMPLATE_BASE_PATH
result = enjoy.render(
tempStr="index.html", # 模板文件:./templates/index.html
data=render_data # 渲染数据Python字典
)
# 5. 输出结果
print("\n=== 模板渲染结果 ===")
print(result)
except RuntimeError as e:
print(f"\n执行失败: {e}")
finally:
# 6. 关闭资源
if 'enjoy' in locals():
enjoy.close()
```