django v1.6 - 在单个循环中使用两次循环标记

时间:2015-01-27 05:24:42

标签: django django-templates django-views cycle

我正在使用django 1.6版。我有一个for循环,需要{%cycle%}模板标记为每个循环分配不同的值,一个在类级别,另一个在图像源。

{% for item in items %}
   <article class={% cycle '"entry style-grid style-hero"' '"entry style"' ... %}>
   .... some more code ...
   <img src={% cycle '"/static/file.png/"' '"/static/file1.png/"' %}>
{% endfror %}

第一个循环循环似乎工作正常,但第二个循环循环不起作用。 html源显示整个循环块,包括第二个循环块。

我还使用了{%load cycle from future%}这是1.6版本中的新功能,但它仍然无效。

不确定如何让它发挥作用。

编辑:

{% for item in items %}
            <article class={% cycle '"entry style-grid style-hero hero-sm-largest type-post col-sm-12 col-md-6 col-lg-6 col-xl-6 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' %}>

                <div class="ribbon ribbon-pulled ribbon-small ribbon-highlight">
                    <a href="{{item.slug}}">{{ item.name}} </a>
                    <a href="{{item.slug}}"><img src="{{ item.country}}" alt="{{item.country}}" /></a>
                </div>

                <header class="entry-header">
                    <h3 class="entry-title"><a href="{{item.slug}}">{{item.name}}</a> </h3>
                    <div class="entry-meta">
                        <span class="entry-date"><a href="{{item.slug}}">
                        {{item.profession}}</a></span>
                    </div>
                </header>

                <figure class="entry-thumbnail">

                    <a href="{{item.slug}}" class="overlay overlay-primary"></a>

                    <!-- to disable lazy loading, remove data-src and data-src-retina -->


                         <img src={% cycle '"/static/placeholder.gif" data-src="/media/{{item.picture}}" data-src-retina="/media/{{item.picture}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"'  %}>

                    <noscript>
                        <img src="/media/{{item.picture}}" alt="">
                    </noscript>

                </figure>
            </article>
                {% endfor %}
        </div>

1 个答案:

答案 0 :(得分:1)

{% cycle %}模板标记不能是多行的。加入单行。

但无论如何,您的代码无法使用 - {{ variable }}不会在{% cycle %}标记中展开。您应该在此处使用{% if %}标记:

{% if forloop.counter0|divisibleby:"11" %}
   <img src="/static/placeholder.gif" data-src="/media/{{item.picture}}"
           data-src-retina="/media/{{item.picture}}" alt="{{item.name}}">
{% else %}
   <img src="/static/placeholder.gif" data-src="/media/{{item.picture_small}}"
           data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}">
{% endif %}