0

我正在进行我的第一个 JinJa2 项目。我想展示汽车。一辆车可以有一个或多个选项。如果一辆车有一个选择,请展示出来。如果有更多,暂时隐藏它。这是我的代码:

//...
{% 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 %}
//...

由于某种原因,它不起作用。选项总是隐藏的。我究竟做错了什么?

我检查了TutorialDocu和另一个SO-Question但没有任何帮助。

4

1 回答 1

0

对我来说,它工作正常,当我跳过

{% 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 %}

您是否使用car-product-item.html的正确路径?尝试将文件car-product-item.html 放入模板文件夹并将包含行更改为 {% include '/car-product-item.html' ...%}。

于 2017-02-17T13:25:10.347 回答