ChainableUndefined 会使 `is escaped` 测试对 `undefined` 子元素通过
作者: colinrotherham创建于 2026年8月27日更新于 2026年8月27日
import inspect
def test_compatibility_escaped(environment):
template_string = inspect.cleandoc("""
{% macro card(params = {}) %}
{#- Support description as string (with deprecated options) #}
{%- set description = params.description if params.description is mapping else {
"text": params.description if params.description is string else undefined,
"html": params.description if params.description is defined and params.description is escaped else params.descriptionHtml
} -%}
{{- description.html | safe if description.html else description.text }}
{% endmacro %}
""")
text = inspect.cleandoc("""
{{ card({
"description": "Example description"
}) }}
""")
text_safe = inspect.cleandoc("""
{{ card({
"description": "
Example description
" | safe }) }} """) text_nested = inspect.cleandoc(""" {{ card({ "description": { "text": "Example description" } }) }} """) html_nested = inspect.cleandoc(""" {{ card({ "description": { "html": "Example description
" } }) }} """) html_deprecated = inspect.cleandoc(""" {{ card({ "descriptionHtml": "Example description
" }) }} """) assert ( environment.from_string(f"{template_string}\n{text}").render() …内容来源: pallets/jinja