我有一个包含 dict 项目的 .yml 文件:
messages_to_skip:
- service: "test1"
message: "Exception during timer operation execution"
- service: "test2"
message: "Error in pipe 'cpl.service.pipe"
- service: "test3"
message: "Unknown attribute with JSONPath"
当我尝试在 jinja 模板中使用它时
messages_to_skip = [
for message_to_skip in messages_to_skip %}
message_to_skip['service'] }}", "{{ message_to_skip['message'] }}"] {% if not loop.last %}, {% endif %}
endfor %}
> ]
我收到一个错误,与此处的单引号相关(在 test2 中):“pipe 'controlpanel”。 我尝试使用反斜杠,但什么也没有。 请问可以帮忙吗?
我尝试使用反斜杠
这应该有效。
您使用过滤器转义引用:
replace("'", "\\'")
messages_to_skip = [
{% for message_to_skip in messages_to_skip %}
["{{ message_to_skip.service }}", "{{ message_to_skip.message|replace("'", "\\'") }}"]{% if not loop.last %}, {% endif %}
{% endfor %}
]