plonezopeplone-3.x

Plone: how can I know if a repeat loop is empty?


I need to display a friendly message if a repeat loop is empty. Example code:

<ul tal:repeat="data context/qr-list">
    <li><a tal:attributes="href string:det-rec?id=${data/id}" tal:content="data/vaga">description</a></li>
</ul>

When the loop is empty, I will display a message: Sorry, no results.


Solution

  • You could use something like this:

    <tal:block define="items context/qr-list">
        <ul tal:condition="items" tal:repeat="data items">
            <li><a tal:attributes="href string:det-rec?id=${data/id}" tal:content="data/vaga">description</a></li>
        </ul>
        <p tal:condition="not:items">Sorry, no results.</p>
    </tal:block>
    

    Maybe you could use the length attribute of the repeat variable but I have never tested it with something like this. See Repeat variables on the Zope Page Templates Reference.