[Zope] ANNOUCE Dtml-Contract

ender kthangavelu@earthlink.net
Mon, 5 Mar 2001 09:15:13 -0800


Introducing the dtml-contract tag, the swiss army knife of dtml, it 
documents, it validates, it does form processing, it redirects, it reduces 
the amount of dtml you need to write, and if you ask nicely it might even do 
your laundry. All of this and more for that amazing price of free.

ats its heart this utilitarian tag is a form processor based on the ArsDigita 
Community System's ad_page_contract.

take a simple dtml form processing method

<dtml-comment>
   this method changes the myint property of this object 
</dtml-comment>

<dtml-if "REQUEST.has_key('myint')">
   <dtml-try>
	<dtml-call "REQUEST.set('myint', _.int(myint)">
   <dtml-except>
            Please enter an integer value for myint
   </dtml-try>
<dtml-else>
	Please enter an integer value for myint
</dtml-if>

<dtml-call "manage_changeProperties({'myint':myint})">

all of this for a simply trying to get a variable coerced and displaying a 
userful error message to the user, add in a few more variables and the amount 
of code just grows.

sophisiticated zopitista's might say, well just use ZPublishers coercion 
functions, and write
 
<input type="string" name="myint:int">

but there are a couple problems with this.
  1. error messages are unhelpful, zpublisher will short circuit so you won't 
get information about errors with all variables
  2. because this information is transmitted via the http request, its 
vulnerable to url surgery.
  3. its hard to customize and add your own conversion utilities, error 
messages, or error pages. 

so how does dtml-contract save the day?

dtml-contract in action

<dtml-contract>
   this method changes the myint property of this object 
<dtml-params>
   myint:integer
<dtml-exception>
   error_page
</dtml-contract>

with this we've setup a method which is documented, expects a variable myint 
in the request, will automatically try to convert it to an integer, and if 
doesn't exist will return an evaluation of error_page.

we've already cut down on the lines of dtml-code, plus we won't get the 
insertion of extra blanks commonly associated with coding in dtml. now 
imagine extending our simple example to a real app with several form 
variables and the utility of dtml-contract becomes more apparent.

i could wax poetic about the myriad features of dtml-contract, but the day is 
short and i must drive to the python conference so here's a quick laundry 
list.

features
- comes with 10 filters including
    -newobid
    -obid
    -range(type, min, max)
    -optional
    -trim
    -nohtml
    -email
    -date
    -float
    -naturalnum

- comes with extensive in source documentation in javadoc style
- comes with extensive readme docs
- takes optional argument to remove all non-contracted variables from the 
request (request.form to be specific).
- takes optional arguements to redirect instead of writing directly to the 
connection on errors
- sets error information in the error_handlers namespace (as contract_errors)
- has utility functions for registering user defined filters.

act now and i'll even toss in some unittests.

http://www.zope.org/Members/k_vertigo/ZopeProducts/PageContract

cheers

kapil thangavelu

off to the python conference.