выражение tal python с условием

<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>

У меня уже есть класс, который печатает изображение-N на основе индекса цикла, но как мне добавить еще один класс, если длина равна 1? Документация НЕ понятна https://zope.readthedocs.io/en/latest/zopebook/AppendixC.html#tales-python-expressions, там сказано, что можно использовать любое допустимое выражение Python, но синтаксис всегда неверен для меня.


person Sandro Antonucci    schedule 15.07.2020    source источник
comment
Вы должны задать этот тип вопроса на нашем форуме, community.plone.org. Пожалуйста, посетите plone.org/support, чтобы узнать, как лучше всего получить помощь по Plone.   -  person T. Kim Nguyen    schedule 20.12.2020


Ответы (2)


Исправил сам.

                                    <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>
person Sandro Antonucci    schedule 15.07.2020

Вы определяете только один внутри повторения, т. е. каждую итерацию — определяете снаружи:

    <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 {
         /**/
    }
person Dek4nice    schedule 17.07.2020