【问题标题】:tal python expression with condition带有条件的 tal python 表达式
【发布时间】:2020-07-18 03:51:08
【问题描述】:
<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表达式,但语法对我来说总是错误的。

【问题讨论】:

标签: plone tal


【解决方案1】:

自己修好了。

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

【讨论】:

    【解决方案2】:

    您在重复中定义 onlyone,即每次迭代 - 在外部定义:

        <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 {
             /**/
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 2020-10-06
      相关资源
      最近更新 更多