[ZPT] Extending tal:condition, making metal:define

richard@bizarsoftware.com.au richard@bizarsoftware.com.au
Fri, 06 Apr 2001 11:46:27 +1000


richard@bizarsoftware.com.au wrote:
> 
> Evan Simpson wrote:
> >
> > Here's two ideas I had while using ZPT today:
> >
> > 1. Suppose that I want to replace a tag, but only if the replacement
> > value exists, otherwise I want to keep the dummy text.  Right now, this
> > is the best I can do:
> >
> > <span tal:condition="exists:request/foo"
> > tal:replace="request/foo"></span>
> > <span tal:condition="not:exists:request/foo">The Text</span>
> >
> > Ditto for 'content' and 'attributes'.  This isn't that bad, but I
> > thought about extending 'condition', so that you could write:
> >
> > <span tal:condition="replace exists:request/foo"
> > tal:replace="request/foo">The Text</span>
> 
> Another approach would be to have the condition be able to break out of the
> processing for that tag if it fails, and not remove the tag. Something
> like:
> 
>   <span tal:condition="noremove:exists:request/foo"
> tal:replace="request/foo">The Text</span>
> 
> Or maybe a whole new TAL statement is required?
> 
>   <span tal:sticky_condition="exists:request/foo"
> tal:replace="request/foo">The Text</span>
> 
> or
> 
>   <span tal:lazy_replace="request/foo">The Text</span>
> 
> Perhaps what we really want is a way for replace to fail gracefully?
> 
>   <span tal:replace="only_if_exists:request/foo">The Text</span>
> 
> My favourite is a tie between the last two... They're probably easier to
> actually implement, and it's much more obvious what they're doing. Not that
> we'd have to go with 'only_if_exists' as the actual keyword ;)

Just a note: I've implemented a "lazy" keyword so that one can:

  <span tal:replace="lazy:request/foo">The Text</span>
  <input name="foo" value="" tal:attributes="value lazy:request/foo">

I currently have a patched Expressions.py using Shane's fixes to
restrictedTraverse, so a diff to provide this functionality is going to be
unusable. In short though, I modified installHandlers so 'lazy' is
recognised, and added a clause to the "except (AttributeError, KeyError):"
in PathExpr.__call__ with "if self._name == 'lazy': return None".

This works a treat for me - I use it in tal:attributes and tal:replace at
the moment.


    Richard

-- 
Richard Jones
richard@bizarsoftware.com.au
Senior Software Developer, Bizar Software (www.bizarsoftware.com.au)