<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表达式都可以使用,但是语法对我来说总是错误的。
发布于 2020-07-15 23:08:51
我自己修好的。
<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-18 03:51:09
你只需要在repeat内部定义一个,即每次迭代--在外部定义它:
<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 {
/**/
}https://stackoverflow.com/questions/62917896
复制相似问题