From fd7d7c597289500f2ec3f0badaea75c40d404ba7 Mon Sep 17 00:00:00 2001 From: mrzhou Date: Wed, 4 Mar 2026 14:40:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20addSharedFunction=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=85=B1=E4=BA=AB=E6=A8=A1=E6=9D=BF=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=81=9A=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enjoy/__init__.py | 38 +++++++++++++++++++++++++++++++- enjoy/templates/layout-test.html | 32 +++++++++++++++++++++++++++ enjoy/templates/layout.html | 17 ++++++++++++++ pyproject.toml | 2 +- setup.py | 2 +- 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 enjoy/templates/layout-test.html create mode 100644 enjoy/templates/layout.html diff --git a/enjoy/__init__.py b/enjoy/__init__.py index a8ccaf5..be6b7b3 100644 --- a/enjoy/__init__.py +++ b/enjoy/__init__.py @@ -110,6 +110,41 @@ class Enjoy: except Exception as e: raise RuntimeError(f"调用失败: {e}") from e + def addSharedFunction(self, layouts: [str]): + """添加全局共享函数""" + for layout in layouts: + self._engine.addSharedFunction(layout) + + def invoke_engine_method(self, method_name: str, *args, **kwargs) -> Any: + """ + 通用方法:调用内部_engine的其他可调用方法 + + :param method_name: 要调用的_engine方法名 + :param args: 位置参数 + :param kwargs: 关键字参数 + :return: 方法调用结果 + :raises RuntimeError: 如果Engine未初始化或方法调用失败 + """ + if not self._engine: + raise RuntimeError("Engine未初始化,请检查Jar包路径") + + try: + # 获取_engine的方法 + method = getattr(self._engine, method_name) + # 转换Python参数为Java兼容类型 + converted_args = [self._convert_python_to_java(arg) for arg in args] + converted_kwargs = {k: self._convert_python_to_java(v) for k, v in kwargs.items()} + # 调用方法并返回结果 + return method(*converted_args, **converted_kwargs) + except AttributeError: + raise RuntimeError(f"Engine对象没有方法: {method_name}") from None + except jpype.JException as e: + print(f"\n=== JFinal Enjoy {method_name} 方法调用异常详情 ===") + e.printStackTrace() + raise RuntimeError(f"{method_name}方法调用失败: {e.getMessage()}") from e + except Exception as e: + raise RuntimeError(f"{method_name}方法调用失败: {e}") from e + def close(self): """关闭JVM,释放资源""" if self._jvm_started: @@ -136,6 +171,7 @@ if __name__ == "__main__": template_base_path=TEMPLATE_BASE_PATH ) + enjoy.addSharedFunction(['layout.html']) # 3. 准备模板参数 render_data = { "name": "JFinal Enjoy 5.2.2测试", @@ -145,7 +181,7 @@ if __name__ == "__main__": # 4. 渲染模板(tempStr是模板文件名,相对于TEMPLATE_BASE_PATH) result = enjoy.render( - tempStr="index.html", # 模板文件:./templates/index.html + tempStr="layout-test.html", # 模板文件:./templates/index.html data=render_data # 渲染数据(Python字典) ) diff --git a/enjoy/templates/layout-test.html b/enjoy/templates/layout-test.html new file mode 100644 index 0000000..e583bd7 --- /dev/null +++ b/enjoy/templates/layout-test.html @@ -0,0 +1,32 @@ +#@layout() +### 调用 layout.html 中定义的模板函数 layout() + +#define main() +
+ 这里是模板内容部分,相当于传统模板引擎的 nested 的部分 + #(name) + +
+ #for(i : hobbies) + #(i) + #end + +
+ #for(i : scores) + #(i) + #end + +
+ #for(i : user) + #(i.key) #(i.value) + #end +
+#end + +#define css() + 这里可以引入额外的 css 内容 +#end + +#define js() + 这里可以引入额外的 js 内容 +#end \ No newline at end of file diff --git a/enjoy/templates/layout.html b/enjoy/templates/layout.html new file mode 100644 index 0000000..a042d22 --- /dev/null +++ b/enjoy/templates/layout.html @@ -0,0 +1,17 @@ +#define layout() + + + + #@css?() + + + +
+ #@main() +
+ + + #@js?() + + +#end \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 00b9b5a..1460905 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "py-enjoy" -version = "0.1.2" +version = "0.1.3" authors = [ { name = "mrzhou", email = "mrzhou@miw.cn" } ] diff --git a/setup.py b/setup.py index d594e0f..3e26983 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name="py-enjoy", - version="0.1.2", + version="0.1.3", packages=find_packages(), include_package_data=True, package_data={