[Zope-dev] Re: ZPT and else

Brent Hendricks brentmh@ece.rice.edu
Fri, 10 May 2002 08:51:12 -0500


Quoting "Jeffrey P Shell":
  > 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>

Another way to do this is to use the python test() function inside
tal:replace to conditionally replace the "if" content with the "else"
content.  For example:

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

    <h3>Edit Menu Items</h3>
    <form action="Delete" method="post" name="actForm"
          tal:replace="python:test(editItems, default, 'No menu items
available')">

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

    </form>
   </div>

This technique works well when you have one case which is more
"involved" and can be the default, and another case which is simple text
(like an error message).


-- Brent

-------------------------------------------------------------------------

"The programmer, like the poet, works only slightly removed from pure
   thought-stuff.  He builds his castles in the air, from air, creating
   by exertion of the imagination.  Few media of creation are so
   flexible, so easy to polish and rework, so readily capable of
   realizing grand conceptual structures."
                          -- Frederick Brooks, Jr., The Mythical Man Month