Python 显示不同的<;部门>;取决于数组长度
我正在进行我的第一个JinJa2项目。 我想展示汽车。汽车可以有一个或多个选择。 如果一辆车有一个选择,展示它。如果有更多,暂时隐藏起来。 这是我的密码:Python 显示不同的<;部门>;取决于数组长度,python,html,python-2.7,jinja2,lektor,Python,Html,Python 2.7,Jinja2,Lektor,我正在进行我的第一个JinJa2项目。 我想展示汽车。汽车可以有一个或多个选择。 如果一辆车有一个选择,展示它。如果有更多,暂时隐藏起来。 这是我的密码: //... {% set products = ['Audi', 'BMW', 'Mercedes', 'Porsche'] %} {% set options = ['Small', 'Sport', 'Coupe', 'Jeep'] %} {% for product in products %} {% include '../
//...
{% set products = ['Audi', 'BMW', 'Mercedes', 'Porsche'] %}
{% set options = ['Small', 'Sport', 'Coupe', 'Jeep'] %}
{% for product in products %}
{% include '../car-product-item.html' with context %}
{% endfor %}
{% if options|length > 1 %}
//If a car has more options, hide this options for the moment
<div class="order-more-options" style="display: none; background: green">
{% for product in options %}
{% include '../car-product-item.html' with context %}
{% endfor %}
</div>
{% elif options|length == 1 %}
//If a car has one option, show this option
<div class="order-more-options" style="display: block; background: orange">
{% for product in options %}
{% include '../car-product-item.html' with context %}
{% endfor %}
</div>
{% endif %}
//...
/。。。
{%set产品=['Audi'、'BMW'、'Mercedes'、'Porsche']%}
{%设置选项=['Small'、'Sport'、'Coupe'、'Jeep']%}
{products%中产品的%s}
{%include'../car product item.html'与上下文%}
{%endfor%}
{%如果选项|长度>1%}
//如果汽车有更多选项,请暂时隐藏此选项
{选项%中的产品为%1}
{%include'../car product item.html'与上下文%}
{%endfor%}
{%elif选项|长度==1%}
//如果汽车有一个选项,则显示此选项
{选项%中的产品为%1}
{%include'../car product item.html'与上下文%}
{%endfor%}
{%endif%}
//...
由于某种原因,它不起作用。选项总是隐藏的。
我做错了什么
我检查了一个、一个和另一个,但没有任何帮助。对我来说,当我跳过
{% include ...
使用以下代码的命令:
{% set products = ['Audi', 'BMW', 'Mercedes', 'Porsche'] %}
{% set options = ['Small', 'Sport', 'Coupe', 'Jeep'] %}
{% if options|length > 1 %}
<div>more than one options</div>
<div class="order-more-options" style="display: none; background: green">
{% for product in options %}
{{product}}
{% endfor %}
</div>
{% elif options|length == 1 %}
<div>exactly one option</div>
<div class="order-more-options" style="display: block; background: orange">
{% for product in options %}
{{product}}
{% endfor %}
</div>
{% endif %}
{%set产品=['Audi'、'BMW'、'Mercedes'、'Porsche']%}
{%设置选项=['Small'、'Sport'、'Coupe'、'Jeep']%}
{%如果选项|长度>1%}
不止一个选项
{选项%中的产品为%1}
{{product}}
{%endfor%}
{%elif选项|长度==1%}
只有一个选择
{选项%中的产品为%1}
{{product}}
{%endfor%}
{%endif%}
您是否使用正确的car-product-item.html路径?尝试将文件car-product-item.html放入templates文件夹,并将包含行更改为{%include'/car-product-item.html'…%}。对我来说很好,您应该打印选项/产品,以确保它不是空的(如果实际情况更复杂)。还要检查
include
是否将您的代码添加到循环之外/if以确保它不是空的。数组中有项目,我检查了它