[ZPT] If/else?

Chris Withers chrisw@nipltd.com
Thu, 10 May 2001 10:08:27 +0100


Guido van Rossum wrote:
> 
> Yes there's no reason to worry.  

:-)

> Our latest proposal will remove these again, and add a
> simpler mechanism instead: "x | y | z" will evaluate to the path x if
> it is defined and non-empty, otherwise to y if it is defined and
> non-empty, otherwise to z (which must be defined, but may be empty).
  ^^^^^^^^^
  should that be just 'empty'?

> We also propose the special value 'default' which can be used (for
> example) as the last element in a |-separated sequence: it means to
> use the "sample" value that's present in the template.

This looks very cool.

> We (Jim, Evan, Ethan, and me) believe that this proposal is much more
> intuitive, but slightly less powerful than the current hacks, because
> it doesn't distinguish between an undefined value and an empty
> ("false") value. 
> Experience with DTML (which also doesn't make this
> distinction in dtml-if) suggests that this is usually fine.

Yeah, I'd agree with that, but it shouldn't do a blanket exception catch either,
like dtml-if used to ;-)

> For those cases where the |-separated sequence doesn't have enough
> expressive power, we propose to fall back on Python expressions
> ("python:<expression>") which are all-powerful,

Are these in there already? I hope not, if fact, please god no!!! :-(
'expr=' was the beginning of the end for DTML and python directly in ZPT is
metaphorically equivalent, for me, to allowing {} as a block delimiter in python
;-)

> or Python scripts. 

Python Scripts are good and make people encapsulate their logic in good,
byte-sized (haha) chunks. I'd be all in favour of forcing people to use these,
even though I don't really believe in forcing people to do anything...

> cond(<expr>, <expr>, <expr>) which serves as a simple if-then-else,

Is this like x ? y : z in C? If so, could it make it into Python full stop? That
would be 'way cool' :-)

> exists(<path>) which tests for a path existence, 

That would be _extremely_ useful :-)))

> and path(<path>)
> which encapsulates a path expression.

Sorry, what does that do? If it's something like:

def path(context,path):
  return context.restrictedTraverse(path)

...then that's even more useful than exists and I hope it can make it into the
RestrictedPython stuff.

Slightly OT, but I'm still dying to be able to do both of the following:

<dtml-var folder/myobject>
&dtml-url-/x/y/object;

How hard would they be to do?

cheers,

Chris