[Zope] Re: Tal HowTo: <dtml-in><dtml-else></dtml-in>

Evan Simpson evan@4-am.com
Thu, 10 Apr 2003 09:41:09 -0500


Florian Konnertz wrote:
> I wondered about this some weeks ago, also, here are my notes about it:
> http://openspirit.homelinux.net/noowiki/zope/Condition

(Moving to ZPT list)

Thanks!  Re-reading Guido's post gave me an idea:

<ul tal:condition-group="flavor" tal:repeat="variety icecreams">
   <li tal:condition="flavor python:variety in ('Cherry', 'Strawberry')">
I like <span tal:replace="variety">this flavor</span> very much.
   </li>
   <li tal:condition="flavor python:variety in ('Vanilla', 'Chocolate')">
I like <span tal:replace="variety">this flavor</span>.
   </li>
   <li tal:condition="flavor python:variety in ('Coffee', 'Mint Chip')">
I don't like <span tal:replace="variety">this flavor</span>.
   </li>
   <li tal:condition="flavor default">
I haven't tried <span tal:replace="variety">this flavor</span>.
   </li>
</ul>

So, tal:condition-group sets up a variable and a scope.  Within that 
scope, all tal:condition statements that precede the conditional 
expression with the group name and whitespace are grouped -- once one of 
them has evaluated true, all of the following ones will short-circuit 
evaluate to false.  The variable will be false until one of the grouped 
conditions is true, and true thereafter.  It can be used to negate the 
group condition or combine it with other conditions.

There are two ways to say "else".  One is to use a true-valued literal, 
as in 'fred default'.  The other is 'not:fred'.  The difference is that 
'fred default' *also* sets 'fred' true, while 'not:fred' does not.  You 
can also spell "notelse", that is, test whether any prior condition 
succeeded, using plain 'fred'.

The question arises of how this would interact with a nested tal:repeat. 
  I suggest that if one wants to "reset" the group within the 
tal:repeat, one must re-declare the group there.  Example:

<tal:x condition-group="fred">
   <tal:x condition="fred default">Always</tal:x>
   <tal:loop repeat="n python:range(3)" condition-group="fred">
     <tal:x condition="fred repeat/n/first">First</tal:x>
     <tal:x condition="not:fred">Not First</tal:x>
     <tal:x condition="fred repeat/n/last">Last</tal:x>
   </tal:loop>
   <tal:x condition="fred default">Never</tal:x>
   <tal:x condition="fred">Always</tal:x>
</tal:x>

...would produce:

   Always
     First
     Not First
     Not First
     Last
   Always

Note the subtle difference between using 'not:fred' and 'fred default' 
for "Not First".  If I had used the latter, 'fred' would become true, 
and 'fred repeat/n/last' would never succeed.

Cheers,

Evan @ 4-am