[ZPT] If/else?
Guido van Rossum
guido@digicool.com
Wed, 09 May 2001 18:15:15 -0500
> Evan Simpson wrote:
> >
> > <div tal:replace="python:if(doChange(), ..., default)">
>
> <comment state="tired" accuracy="probably way off the mark">
> All this stuff in ZPT looks like it's becoming even more complicated that DTML,
> I'm sure that's a bad thing :-S
> </comment>
>
> I know Jim's going to be worrying about this, and hopefully everyone else is
> too...
>
> Is this unfounded paranoia on my part?
>
> Chris
Yes there's no reason to worry. Jim did worry about this in our
meeting today as we were considering ever more complicated changes to
the path expression syntax in order to accomplish a few simple user
goals: actions conditional on the validity of a given path expression,
and we found a nice way out that actually simplifies the TALES path
expression syntax!
Evan's latest hacks for this added "(if)" and "(if exists)" to the
path syntax. 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).
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.
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.
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, or Python scripts. In
order to make testing of paths in Python expressions more convenient,
we propose adding some simple functions to Python (in the context of
TALES Python expressions and Zope Python scripts) like
cond(<expr>, <expr>, <expr>) which serves as a simple if-then-else,
exists(<path>) which tests for a path existence, and path(<path>)
which encapsulates a path expression.
--Guido van Rossum (home page: http://www.python.org/~guido/)