2

我有一个 django 模板,我需要在两个不同的 for 循环中循环一组背景颜色。循环标签似乎被设计为在一个 for 循环内或完全在一个 for 循环外使用。这是我的代码:

{% if global_adverts %}
        <span style="display:none">{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors %}</span>
        {% for advert in global_adverts %}
            <div class="{% cycle adcolors %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle adcolors %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

有没有办法做到这一点,而无需在第一个循环之前输出循环中的第一项并且必须用 css 隐藏它?

4

3 回答 3

1

只需添加静音

{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors silent %}

它阻止标签输出值

评论后编辑

如果您需要为每个循环继续循环,只需使用循环两次并更改第二个循环中项目的顺序:

{% if global_adverts %}
        {% for advert in global_adverts %}
            <div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle 'advert-pale-blue' 'advert-green' 'advert-blue' 'advert-grey' %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}
于 2013-10-17T20:14:31.310 回答
1

我发现以下代码给出了预期的结果:

{% if global_adverts %}
        {% for advert in global_adverts %}
            <div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolours %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle adcolours %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}
于 2013-10-18T10:26:58.920 回答
0

我觉得{% cycle %}在循环之外有第一个声明更干净,我不希望条件循环内部的声明可以工作,这里是如何让它工作:

{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolour silent %}

{% if global_adverts %}
    {% for advert in global_adverts %}
        <div class="{{ adcolours }}">
            {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                <p>{{ advert.text }}</p>
            {% if advert.url %}</a>{% endif %}
        </div>
        {% cycle adcolours %}
    {% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
    {% if adverts %}
        {% for advert in adverts %}
            <div class="{{ adcolours }}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
            {% cycle adcolour %}
        {% endfor %}
    {% endif %}
{% endwith %}

由于{% cycle adcolour %}是静默的,我们使用{{ adcolour }}打印当前颜色。我们仍然需要{% cycle adcolour %}在每次迭代中使用静默来逐步浏览颜色。

于 2019-07-30T02:29:23.977 回答