Home arrow Zope arrow Page 8 - ZPT Basics (part 2)

Submitting To The King - Zope

Dig deeper into the world of ZPT with examples andillustrations of how to create, manipulate and use variables andconditional tests in your templates.

  1. ZPT Basics (part 2)
  2. A Little Male Bonding
  3. The Number Game
  4. A Question Of Scope
  5. Switching Things Around
  6. Comparing Apples and Oranges
  7. If It's Thursday, It Must be Italy
  8. Submitting To The King
  9. Looking Ahead
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 13
September 25, 2002

print this article


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.

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: