[ZPT] Re: TAL's lack of an else...

Fergal Daly fdaly at sift.co.uk
Fri Aug 1 17:12:25 EDT 2003


On Friday 01 August 2003 15:21, Markus Kemmerling wrote:
> There's no else in XSLT ;-) But there is a choose/when/otherwise.
> I would appreciate to have a similar construct in TAL, but I don't know how
> to do it in a simple way, although you could write:
>
> <ul tal:choose="flavour" tal:repeat="variety icecreams">
>     <li tal:when="flavour python:variety in ('Cherry', 'Strawberry')">
> I like <span tal:replace="variety">this flavor</span> very much.
>     </li>
>     <li tal:when="flavour python:variety in ('Vanilla', 'Chocolate')">
> I like <span tal:replace="variety">this flavor</span>.
>     </li>
>     <li tal:when="flavour python:variety in ('Coffee', 'Mint Chip')">
> I don't like <span tal:replace="variety">this flavor</span>.
>     </li>
>     <li tal:otherwise="flavour">
> I haven't tried <span tal:replace="variety">this flavor</span>.
>     </li>
> </ul>

That is essentially switch/case which would also be useful but it seems to 
require a call out to python in order to do the comparisons. Strangely Perl 
(a language which has no fear of building things in) has no builtin switch 
construct, although the next version will have one (of course).

At the moment people are usually told to just use if/elsif/else, it's not too 
hard to simulate switch/case with if/elsif/else, so assuming TAL could do it, 
the above would be

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

Which is actually less typing!

> At 13:55 01.08.2003 +0100, Fergal Daly wrote:
> ><tal:x tal:if-else>
> >   <tal:x tal:if="user/isLoggedIn">
> >     welcome
> >   </tal:x>
> >   <tal:x tal:else>
> >     please login
> >   </tal:x>
> ></tal:x>
>
> That won't work: In a wellformed document every attribute must have a
> value.

oops, that's a pity, make it

tal:if-else=""

tal:else=""

I suppose, although that's a bit of a pain. All ways of doing this are ugly 
but I still think we can find something better than

<tal:x tal:define="temp user/isLoggedIn">
  <tal:x tal:condition="temp">
    welcome
  </tal:x>
  <tal:x tal:condition="not:temp">
    please login
  </tal:x>
</tal:x>

Which is the way it's currently done,

F




More information about the ZPT mailing list