[ZPT] FORMs and processing

Jørgen Hermanrud Fjeld jhf@hex.no
Fri, 29 Jun 2001 01:27:36 +0200


Hi.

I use a slightly different approach, because I want each PT to be 
more-or-less wysiwyg for the designers. 
Example:
form_1.html <PT>
acknowledge_1.html <PT>
script_1 <Script>

But the Script checks for variables, processes, and return the appropriate PT 
with something like:
******************
r=context.REQUEST
f=context.REQUEST.form
if f.has_key('post'):
	# ......Code to process the form, i.e. call SQL methods with data......
	r.set('name','John Doe')
	if result == 1:
		return context['acknowledge_1.html'](context)
r.set('categories',context.SQLMethod()[0])
return context['form_1.html'](context)
*******************
That way the designer has minimal with conditional tags, and knows what the 
pages look like, and if I want to have several different pages depending on 
how the processing went, then it can easily be done because the script 
returns the correct PT depending on some information in the 
REQUEST/processing of the request.

Lets say I have a series of pages, a knowledge tour, the first page selects 
what kind of tour, then there are som pages with the questions, in some 
special order, then there is a summary page.
So I could have a "settings.html", "tour1.html", "tour2.html", "tour3.html", 
"summary.html". When accessing the script, the correct PT is rendered, and 
the designers don't think about code in their work. From their standpoint, it 
just happens in the correct order, as long as all "POST" actions go to the 
script.

It is also easy to fetch informtion to be rendered in the PT in the script, 
and process the information so that there is a minimum of code tags in the PT 
itself.  As shown above in the script.

What do you guys think of this approach?

Sincerely 
Jørgen

torsdag 28. juni 2001 06:19, skrev jmr@computing.com:
> tony.mcdonald> What's the received wisdom on using FORMs and
> tony.mcdonald> PageTemplates? After a nightmare project where HTML
> tony.mcdonald> coders were mangling DTML code I've decided to look at
> tony.mcdonald> ZPT in a lot more detail...
>
> The paradigm I use a lot goes like this:
>
> One form (PT) and one associated python script.  The PT is the one
> that gets published.  The PT either displays the form or displays the
> success message.  The action on the form goes back to itself.
>
> The PT calls the python script.  The script returns a dictionary which
> includes a "success" variable, perhaps a "results" variable (if there
> is data to get displayed afer successful posting, and either a simple
> error variable or an error dictionary (keyed by form field name).  All
> this stuff determines how the PT will render itself.
>
> The script checks to see if the form has been submitted before (I use
> the existance of a hidden variable as a flag) and if so, validates the
> data.  If it hasn't been filled in, in some cases it may prefill some
> fields.
>
> If the data is not valid, the form is redisplayed with error messages
> as appropriate... if it is valid, a success message is displayed
> instead.
>
> Typically looks something like this:
>
> This example is about creating accounts; has optional output for genera=
l
> errors, errors per field, debug output..
>
> <html...head...body>
>    <div metal:fill-slot="main"
>      tal:define="script_output here/accounts/adduser">
> 	<div tal:condition="script_output/success | nothing">
> 	  <div tal:replace="structure script_output/debug | nothing">
> 	    a debug message would go here if there was one returned...
> 	  </div>
> 	  <h3>Your account has been created.</h3>
> 	  <span tal:condition="not:request/no_mail | default">
> 	    <p>
> 	      A random password has been generated and was mailed to the
> 	      address you gave (<span tal:replace="request/mail">
> 	=09joeuser@someplace.com</span>).</p>
> 	    <p>After you have received your temporary password via email,
> 	      you may go <a href="accounts/change_password.html">here</a>
> 	      to change it.</p>
> 	  </span>
> 	</div>
>
> 	<form action="account_form.html" method="post"
> 	      tal:condition="not:script_output/success | default">
> 	  <input type=hidden name="kilroy"
> 		 tal:attributes="value request/URL0">
> 	  <h2 align=center>Create New Account</h2>
> 	  <span tal:condition="script_output/errors/general | nothing">
> 	    <div align=center tal:content="script/output/errors/general"=
></div>
> 	  </span>
> 	  <table border="0" cellpadding="3" cellspacing="5" width="7=
20"
> align="center"> <tr>
> 	      <td><b>User id </b> (We suggest your Email Address)</td>
> 	      <td><input type="text" name="uid" size="24"
> 			 tal:attributes="value request/uid | nothing">
> 		  <span tal:condition="script_output/errors/uid | nothing"
> 		    tal:content="script_output/errors/uid"></span></td>
> 	    </tr>
> 	    <tr>
> 	      <td><b>Email Address </b> (Must be a good address)</td>
> 	      <td><input type="text" name="mail" size="24"
> 			 tal:attributes="value request/mail | nothing">
> 		  <span tal:condition="script_output/errors/mail | nothing"
> 		    tal:content="script_output/errors/mail"></span></td>
> 	    </tr>
> 	    ...
>
>
> (As I go over this, I find some optimizations I could make.... but it
> gets the concept across.  :)
>
>
> Jim Rowan
> DCSI
> jmr@computing.com
>
> _______________________________________________
> ZPT mailing list
> ZPT@zope.org
> http://lists.zope.org/mailman/listinfo/zpt

-- 

MVH
Jørgen H. Fjeld