"""测试预览API""" import requests # 测试简单模板 body1 = """{% set greeting = ['嗨', '哈喽'] %} {{ greeting | random }},小优来啦!""" print("测试1: 简单模板") r = requests.post('http://localhost:8000/api/templates/preview', json={ 'template_body': body1 }) print(r.json()) print() # 测试完整x11模板 body2 = """{% set program_name_map = {'1': '棉麻'} %} {% set program_id = program_id | string | trim %} {% set program_name = program_name_map.get(program_id) %} {% if program_name %} {{ period }}好呀,为你推荐{{ program_name }}程序! {% else %} 程序不存在 {% endif %}""" print("测试2: 带变量的模板") r = requests.post('http://localhost:8000/api/templates/preview', json={ 'template_body': body2 }) result = r.json() print(f"成功: {result['success']}") print(f"结果: {result['result']}") print(f"使用的变量: {list(result['context_used'].keys())}")