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

Markus Kemmerling markus.kemmerling at mediaweb.at
Fri Aug 1 19:21:28 EDT 2003


At 16:12 01.08.2003 +0100, Fergal Daly wrote:
>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,

And in XSLT people are told to use choose if they want an else. I think you 
don't have an else in XSLT because you don't have indentation (or 
parantheses) for grouping. That's because it is XML and similar to TAL in 
this respect (allthough it is a tag language and not an attribute language).

If you only have a simple true/false-alternative you can go the 
*define/condition true/condition false* way in XSLT, too. But if you have 
more possibilities you need *choose/when/otherwise* or 
*condition-group/condition/condition="default"* or *group/if/else* or 
*if/elif/else* or *if-else/if/else* or whatever. I can't see any big 
difference besides the syntax. (I have to admit that I don't understand the 
subtelties of condition-group yet.)

If you would like to reproduce the example above in TAL now, you would have 
to write for the default case:

<li tal:condition="python:variety not in ('Cherry', 'Strawberry', 
''Vanilla', 'Chocolate', Coffee', 'Mint Chip')">

That's really ugly. So you better write a python script ...

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

You're right to leave out the variable (flavour). I don't understand why 
you would need it anyway. But if I see the empty attribute values, I still 
prefer

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

Markus Kemmerling 




More information about the ZPT mailing list