Jinja 在 if-else 之后追加在同一行
Jinja append in same line after if-else
我有下面这一段HTML:
<li style="margin-bottom: 3px; margin-top: 3px">Vendor accounts:</li>
{% if fact_foundVendorList is defined and fact_foundVendorList|length > 0 %}
{% for vendor in fact_foundVendorList %}
<ul>
<li style="margin-bottom: 3px; margin-top: 3px">{{ vendor }}</li>
</ul>
{% endfor %}
{%- else -%}
No accounts were found for the user
{%- endif -%}
当 if fact_foundVendorList
returns 为假时,它看起来像这样:
* Vendor accounts:
No vendor accounts were found for the user
否则,它会像这样:
* Vendor accounts:
* account1
* account2
我只想更改 false
输出。如果它是假的,我想在与“供应商帐户”行相同的行中显示消息。所以它应该是这样的:
* Vendor accounts: No accounts were found for the user.
我认为添加以下内容 %- -%
会奏效,但我认为我需要其他内容
{%- else -%}
No accounts were found for the user
{%- endif -%}
我在网上看到了各种相关的解决方案,但无法在不破坏模板中 <ul>
和 <li>
标签结构的情况下将其应用到我的案例中。文档很长。
你的问题在于你必须在包含 Vendor accounts:
的 <li>
中包含 No accounts were found for the user
,否则它不会在同一行,因为您希望您的列表是默认的 display: list-item
类型。
那么,您可以做的是创建一个带有条件的变量,然后用它来显示消息。
{% set has_account = fact_foundVendorList | default([]) | length > 0 %}
<li style="margin-bottom: 3px; margin-top: 3px">
Vendor accounts: {% if not has_account %}No accounts were found for the user{% endif %}
</li>
{% for vendor in fact_foundVendorList | default([]) %}
<ul>
<li style="margin-bottom: 3px; margin-top: 3px">{{ vendor }}</li>
</ul>
{% endfor %}
注意:通过将default
值定义为空列表,您无需断言该列表是否定义为length
过滤才能给你正确的信息
我有下面这一段HTML:
<li style="margin-bottom: 3px; margin-top: 3px">Vendor accounts:</li>
{% if fact_foundVendorList is defined and fact_foundVendorList|length > 0 %}
{% for vendor in fact_foundVendorList %}
<ul>
<li style="margin-bottom: 3px; margin-top: 3px">{{ vendor }}</li>
</ul>
{% endfor %}
{%- else -%}
No accounts were found for the user
{%- endif -%}
当 if fact_foundVendorList
returns 为假时,它看起来像这样:
* Vendor accounts:
No vendor accounts were found for the user
否则,它会像这样:
* Vendor accounts:
* account1
* account2
我只想更改 false
输出。如果它是假的,我想在与“供应商帐户”行相同的行中显示消息。所以它应该是这样的:
* Vendor accounts: No accounts were found for the user.
我认为添加以下内容 %- -%
会奏效,但我认为我需要其他内容
{%- else -%}
No accounts were found for the user
{%- endif -%}
我在网上看到了各种相关的解决方案,但无法在不破坏模板中 <ul>
和 <li>
标签结构的情况下将其应用到我的案例中。文档很长。
你的问题在于你必须在包含 Vendor accounts:
的 <li>
中包含 No accounts were found for the user
,否则它不会在同一行,因为您希望您的列表是默认的 display: list-item
类型。
那么,您可以做的是创建一个带有条件的变量,然后用它来显示消息。
{% set has_account = fact_foundVendorList | default([]) | length > 0 %}
<li style="margin-bottom: 3px; margin-top: 3px">
Vendor accounts: {% if not has_account %}No accounts were found for the user{% endif %}
</li>
{% for vendor in fact_foundVendorList | default([]) %}
<ul>
<li style="margin-bottom: 3px; margin-top: 3px">{{ vendor }}</li>
</ul>
{% endfor %}
注意:通过将default
值定义为空列表,您无需断言该列表是否定义为length
过滤才能给你正确的信息