[Zope] Re: Newbie: TAL Conditions

Duncan Booth duncan.booth at suttoncourtenay.org.uk
Tue Dec 7 09:51:21 EST 2004


 wrote:

> This works fine.  However, where there isn't a record (i.e. an empty 
> string), a <TD> will not be defined so that it leaves ugly gaps in the 
> table.  To ensure that a '<TD>&nbsp;</TD>' replaces an empty string '' I 
> added two tal:condition statements.  This works, but is sort of ugly 
> looking as, in my head, it is the equivalent of two 'if' statements when 
> it should be an 'if/else'.  The below code works, however is there a 
> cleaner way to do this?
> 
> ### START CODE ###
> 
><table border="1">
>  <tr><th>Date</th><th>Server</th></tr>
>  <tr tal:repeat="transfer container/getTransfers">
>     <td tal:content="structure transfer/time_stamp">time_stamp</td>
>     <td tal:condition="python: transfer.server==''">&nbsp;</td> 
>     <td tal:condition="python: transfer.server!=''"
>         tal:content="structure transfer/server">server</td>
>  </tr>
></table>
> 
> ### END CODE ###

Try this:

<table border="1">
  <tr><th>Date</th><th>Server</th></tr>
  <tr tal:repeat="transfer container/getTransfers">
     <td tal:content="structure transfer/time_stamp">time_stamp</td>
     <td tal:content="structure python: transfer.server or default"
         >&nbsp;</td>
  </tr>
</table>

When transfer.server is false (e.g. None or an empty string), this uses the 
default value i.e. &nbsp;, if transfer.server is any value which is 
considered true it substitutes the value.

For a more complex condition you could use the 'test' function:

     <td tal:content="structure python:test(transfer.server=='', default, 
transfer.server)"
         >&nbsp;</td>

This would use the default only for an empty string, not for other false 
values such as False or None.



More information about the Zope mailing list