更新render_template,确保渲染正常
This commit is contained in:
parent
368ac78d63
commit
41a4eead05
@ -3,8 +3,8 @@
|
||||
|
||||
import jingrow
|
||||
import logging
|
||||
from jinja2 import Environment
|
||||
from jingrow.utils.jingrow_api import get_record_list, get_pagetype_module_app, get_record
|
||||
from jingrow.utils.jinja import render_template
|
||||
|
||||
|
||||
def _get_all_local_ai_agents():
|
||||
@ -99,10 +99,8 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
trigger = True
|
||||
else:
|
||||
try:
|
||||
# 使用与 ai_content_generation 节点一致的 Jinja2 渲染方式(宽松 undefined)
|
||||
# 使用统一的 render_template 进行 Jinja2 条件渲染
|
||||
cond_tpl = str(agent.get("condition") or "").strip()
|
||||
jenv = Environment()
|
||||
jtpl = jenv.from_string(cond_tpl)
|
||||
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
||||
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
||||
_pt = base_ctx.get("pagetype") or getattr(pg, "pagetype", None)
|
||||
@ -112,7 +110,7 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
api_res = get_record(str(_pt), str(_nm))
|
||||
if api_res and api_res.get("success") and isinstance(api_res.get("data"), dict):
|
||||
pg_ctx = api_res.get("data")
|
||||
result = jtpl.render(pg=pg_ctx)
|
||||
result = render_template(cond_tpl, {"pg": pg_ctx})
|
||||
if str(result).strip().lower() in ("true", "1", "yes"):
|
||||
trigger = True
|
||||
except Exception:
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import json
|
||||
from typing import Dict, Any, Optional
|
||||
from jinja2 import Environment
|
||||
|
||||
from jingrow.utils.jingrow_api import get_record
|
||||
from jingrow.utils.jingrow_cloud import call_ai_model
|
||||
from jingrow.utils.jinja import render_template
|
||||
|
||||
def execute(context=None, inputs=None, config=None):
|
||||
"""
|
||||
@ -90,11 +90,9 @@ def execute(context=None, inputs=None, config=None):
|
||||
if key not in template_context or not template_context.get(key):
|
||||
template_context[key] = value
|
||||
|
||||
# 4. 渲染模板(使用Jinja2,容错处理undefined变量)
|
||||
# 4. 渲染模板(使用统一的 render_template,容错处理undefined变量)
|
||||
try:
|
||||
jenv = Environment()
|
||||
jtpl = jenv.from_string(template)
|
||||
prompt = jtpl.render(**template_context)
|
||||
prompt = render_template(template, template_context)
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
最佳实践原则:
|
||||
- 复用单例 Environment,启用 trim_blocks/lstrip_blocks 提升可读性;
|
||||
- 采用 StrictUndefined,缺失变量时报错,避免静默吞错;
|
||||
- 默认采用宽松 Undefined(与 Jinja2 默认一致),便于条件表达式渲染;
|
||||
- 不启用 autoescape(后端逻辑条件渲染,不输出到 HTML)。
|
||||
"""
|
||||
|
||||
@ -22,10 +22,10 @@ def _get_env():
|
||||
if _env is not None:
|
||||
return _env
|
||||
try:
|
||||
from jinja2 import Environment, StrictUndefined
|
||||
from jinja2 import Environment, Undefined
|
||||
_env = Environment(
|
||||
autoescape=False,
|
||||
undefined=StrictUndefined,
|
||||
undefined=Undefined,
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True,
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user