You may remember, from the first part of this series, an exampleinvolving a form and a form processor. In that example, I used two Zopeobjects - a single DTML Document to display the form, and a separatepage template to process the form input and generate appropriate output.However, ZPT provides an elegant method to combine those two pages intoone via the "submit" variable.
In order to better understand this, create a Page Template named"DualPurposeForm" and add the following code to it: <div tal:define="global submit request/form/submit | nothing"tal:condition="not:submit"> <form action="DualPurposeForm" method="POST">Species:<br><input name="species"><p>Home planet:<br><input name="planet"></p><p>Distance (light years) from Earth: <br><input name="distance"></p><p><input type="Submit" name="submit" value="Beam Me Up, Scotty"></p></form> </div><div tal:condition="submit"><p>Welcome to Earth, <b tal:content="request/form/species">alien speciesname</b> from the planet <b tal:content="request/form/planet">planetname</b>. </p><p>How was your journey?Travelling <b tal:content="request/form/distance">distance</b> lightyears must be quite a shock to the system. Why don't you relax and have a drink?</p></div>There are two conditional tests in the template above, both keyed on thepresence or absence of the "submit" variable. The first time thetemplate is accessed, the "submit" variable will not exist, andtherefore the first conditional test will evaluate to true and the formwill be displayed. Once the form has been submitted, the same templatewill be called again. However, this time around, the "submit" variablewill exist in the request context, and so the second part of thetemplate will be displayed.
There are a couple of things to note in the example above. First, thetemplate variable "submit", which is set depending on the presence of aform "submit" variable in the request object. In case this variable doesnot exist, the template variable "submit" is set to the special value"nothing". You may remember, again from the first part of this series,that "nothing" is a special TAL variable used to represent a null value.
Second, the usage of the "not" keyword in the first conditional test.This keyword evaluates the TALES expression that follows, and returnsthe Boolean reverse of the result. Thus, the first time the template isaccessed, the value of the "submit" variable will be set to null, theTALES expression will evaluate to Boolean false and the "not" negationwill convert it to Boolean true...which serves as a flag to display theempty form.
This technique makes it possible to reduce the number of objects used,and perhaps make your Zope object collection easier to handle.