[Zope-dev] ZPT and 'else'

Jeffrey P Shell jeffrey@cuemedia.com
Thu, 09 May 2002 21:10:13 -0600


On 5/9/02 7:07 PM, "Tim Hoffman" <timhoffman@cams.wa.gov.au> wrote:

> Just my 2c worth, but I would like to defend order of execution of
> ZPT. What it does mean for me is I can guaruntee zpt commands will
> "always" be processed in a known order irrespective of where you
> put them in a tag, this I like ;-)

Me too.  Occasional alarms, no surprises.

> What I do miss is "else"  clause  but I think it would be probably
> be too hard to implement, and too much of encouragement for people
> to start putting more logic in the template, so on the whole it
> is probably best to leave it out.
> 
> See ya
> 
> T
> On Fri, 2002-05-10 at 05:15, Jim Penny wrote:
>> My most-missed DTML feature has not been mentioned at all -- it is not
>> loop batching -- it is the dtml-else option of dtml-in -- which made it
>> much easier to handle the "nothing found" case.

'else' is tricky within the block oriented structure of anything XML-ish,
because of the concept of 'well-formedness'.  The 'if' statement would have
to be singly wrapped, and the else block wrapped separately, looking at
least somewhat awkward any way you go about it.  The best I can come up with
in my mind is this, in order to have the 'else' pick up on the condition
expressed in its surrounding container.  But, yuck:

<if ...>
 true stuff
 <else>
  false stuff
 </else>
</if>


A good page template way is something like this:

<tal:if condition="myTalesExpression">
 truth
</tal:if>
<tal:else condition="not:myTalesExpression">
 false
</tal:else>

The 'not' TALES namespace is valuable.  The downside is that you evaluate
the expression twice.  A good way to work within this is something that I
did earlier today, outside of this conversation, where I evaluate an
expression earlier and assign it to a variable:

<div id="edit-area"
     tal:define="editItems python:here.getMenuItem(...)">

  <h3>Edit Menu Items</h3>
  <form action="Delete" method="post" name="actForm"
        tal:condition="editItems">

     ... (form and table elements, and a loop over editItems
          contained in here if there were results) ...

  </form>

  <div class="emph"
       tal:condition="not:editItems">
   No menu items available
  </div>

</div>


This is something I did a lot in DTML too, setting a search result to either
a global variable, or inside of a large <dtml-let> namescape

-- 
Jeffrey P Shell
www.cuemedia.com

Industrie Toulouse on Zope
http://radio.weblogs.com/0106123/categories/zope/