0
<tal:block tal:repeat="image project/images">
    <div 
        tal:define="onlyone python:if repeat.image.length==1: return 'onlyone'"
        tal:attributes="class python:'image-{}'.format(repeat.image.number)">
        <img 
            tal:define="img image/getObject" 
            tal:replace="structure img/@@images/image/custom_1280" 
        />
    </div>
</tal:block>

我已经有一个基于循环索引打印“image-N”的类,但是如果长度为“1”,我该如何添加另一个类?文档不清楚https://zope.readthedocs.io/en/latest/zopebook/AppendixC.html#tales-python-expressions,它说可以使用任何有效的python表达式,但语法对我来说总是错误的。

4

2 回答 2

0

您只在重复内定义一个,即每次迭代 - 在外面定义它:

    <div class="project-images"
        tal:define="onlyone python: len(project.images)==1 and 'onlyone' or '';">
        <tal:project-images repeat="image project/images">
            <div tal:attributes="class string:image-${repeat/image/number} ${onlyone};">
                <img
                    tal:define="img image/getObject" 
                    tal:replace="structure img/@@images/image/custom_1280" 
                />
            </div>
        </tal:project-images>
    </div>

但...

您的任务可以(并且应该)在 CSS3 中解决,而无需额外的 html 类:

    .project-images div:first-child:nth-last-child(1) {
         /**/
    }
    .project-images div:only-child {
         /**/
    }
于 2020-07-17T19:51:08.807 回答
0

自己修好了。

                                    <tal:block tal:repeat="image project/images">
                                        <div 
                                            tal:define="onlyone python:'onlyone' if repeat.image.length == 1 else ''"
                                            tal:attributes="class string:image-${repeat/image/number} ${onlyone}">
                                            <img 
                                              tal:define="img image/getObject" 
                                              tal:replace="structure img/@@images/image/custom_1280" 
                                            />
                                        </div>
                                    </tal:block>
于 2020-07-15T15:08:51.337 回答