[Zope] ZPT, DTML, SQL and Forms

Jeff Peterson jpeterso@rangebroadband.com
Tue, 26 Mar 2002 10:29:04 -0600


> I want to have a site that allows adding, editing & searching of various
> database tables (eg. people).
>
> To keep things consistent for the user, I'd like to use one page
> template that has all the fields, and just call that template with an
> add dtml method, an edit dtml method, and a search dtml method.
>
> The part I'm having difficulty with is the connection between the ZSql
> and the template - specifically, I execute a SQL function to get the
> person's data, and I'd like to populate the template with it. To do
> this, I need to set the 'value' field of each input type in the form.
>

Well, you need to execute the ZSQL method obviously, and you can do this
more than one way:

One way to do it is to call it in a Script (python) and pass the information
by the request namespace:

Script (python)
===============

data = context.your_sql([param=value])
#assuming you returned 1 row
context.REQUEST.set('field', data[0]['colname'])
return context.your_template(context)

then

  <tr>
    <td><LABEL for="firstname">First name: </LABEL></td>
    <td><INPUT type="text" name="first_name" value=""
               tal:attributes="value request/field"
               tal:on-error="string:">
    </td>
  </tr>


but this is messy and requires an extra file....

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>

There are probably other ways as well but I'd use method #2.

Hope this helps.

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