我有包含的ag / Django模板
{% for block in page.content %}
{% include_block block %}
{% if "Email address:" in block %}
This is an email address
{% endif %}
{% endfor %}
该块确实包含文本,但是if子句不返回True
怎么了?
问题是在块类型中。
此代码将帮助:
{% for block in page.content %}
{% include_block block %}
{% if "Email address:" in block|striptags %}
This is an email address
{% endif %}
{% endfor %}
{% if "Email address:" in block %}
应为:{% if "Email address:" in block.value %}
。
您从page.content
循环获得的块对象不是一个简单的字符串-它包含block_type
之类的属性以及允许将其呈现为HTML的方法-结果,像in
之类的字符串比较将不会t直接在该对象上工作。
根据所定义的块类型,您可能还需要执行{% if block.block_type == 'text' %}
这样的检查,以确保您没有对基于非字符串的块(例如StructBlock)进行字符串比较。