[Zope] ZPT, DTML, SQL and Forms

Jeff Peterson jpeterso@rangebroadband.com
Tue, 26 Mar 2002 15:38:31 -0600


> -----Original Message-----
> From: Colin Fox [mailto:cfox@crystalcherry.com]
> Sent: Tuesday, March 26, 2002 2:43 PM
> To: jpeterso@rangebroadband.com
> Cc: zope@zope.org
> Subject: RE: [Zope] ZPT, DTML, SQL and Forms
>
>
> On Tue, 2002-03-26 at 08:29, Jeff Peterson wrote:
> <snip>
> >
> > it makes more sense to me to call it in your template:
> >
> >
> > Page Template
> > =============
> > (might have to massage the param/value part if it's needed; get it from
> > request or some other way)
> >
> > <table tal:define="data python:here.your_sql([param=value])">
> >   <tr tal:repeat="items data">
> >     <td><LABEL for="firstname">First name: </LABEL></td>
> >     <td><INPUT type="text" name="first_name" value=""
> >                tal:attributes="value items/colname"
> >                tal:on-error="string:">
> >     </td>
> >   </tr>
> > </table>
>
> The problem with this, though, is that I don't want to call the database
> in my template. I want the template to be re-used by 3 different DTML
> methods: add, edit & search. When adding and searching, I'm not
> executing a database query until the user hits the submit button. But in
> the case of 'edit', I'm passing the personid that I want to edit, so the
> edit dtml method executes the query and then invokes the template.
>
> Here's what my edit method looks like (it's invoked with the parameters
> "?personid=x"):
>
> <dtml-var standard_html_header>
> <h2><dtml-var title_or_id> <dtml-var document_title></h2>
>
> <dtml-in expr="get_customer(pid=personid)">
> <form action="&dtml-URL0;" method="post">
> <input type="hidden" name="personid" value=<dtml-var personid> >
> <dtml-var genericform_html>
> </form>
> </dtml-in>
>
> <dtml-var standard_html_footer>
>

You have 4 choices I can see:

1) use a different form template for when you need to populate the form.
2) Use the same form and conditionalize the sql based on what you are doing
like:

Add to your dtml:
<input type="hidden" name="edit" value="1">

<div tal:condition="python: request.edit == '1'">
<table tal:define="data python:here.your_sql([param=value])">
  <tr tal:repeat="items data">
    <td><LABEL for="firstname">First name: </LABEL></td>
    <td><INPUT type="text" name="first_name" value=""
               tal:attributes="value items/colname"
               tal:on-error="string:">
    </td>
  </tr>
</table>
</div>
<div tal:condition="python: request.edit != '1'">
<table>
  <tr>
    <td><LABEL for="firstname">First name: </LABEL></td>
    <td><INPUT type="text" name="first_name"></td>
  </tr>
</table>
</div>


3) Use the Script (python) I showed you before, setting the values into the
request namespace.

4) If you are using Page Template you can put the form function in a macro
and use one template but make the conditional call the macro based on what
you want to do.

I'd choose either the Macro method or make a separate template.  Ive used
the latter o great success.

--
Jeffrey D. Peterson
Webmaster & Resident Standards Warrior
"The Trouble with doing anything right the first time is that nobody
appreciates how difficult it was."